🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: gnu.g++.lib.bug
2 messages
2 total messages Started by Jose Cordova Wed, 07 Feb 1990 23:28
Run-time error when using 'get' with an istream object
#572
Author: Jose Cordova
Date: Wed, 07 Feb 1990 23:28
25 lines
1137 bytes
I am having trouble using the 'get' method for the 'istream' class.
It generates a 'Segmentation fault' error at run-time.  I know the
'istream' is being opened correctly because reading into a 'String'
object with >> works fine.  Am I doing something wrong ?
The sample program and "data" illustrate the point:

main()
{
    istream from("data",io_readonly,a_useonly);
    char *line;

    from.get(line,15);
    cout << line;
}

Sample "data" file contents:
word1 word2
word3

Other relevant information:
SunOS 4.0.3         libg++ version 1.35.0        g++ version 1.35.0

Jose Cordova, CS Dept., Mississippi State Univ., jose@larry.cs.msstate.edu
   
 
Run-time error when using 'get' with an istream object
#576
Author: Doug Lea
Date: Thu, 08 Feb 1990 10:48
59 lines
2154 bytes
> I am having trouble using the 'get' method for the 'istream' class.
> It generates a 'Segmentation fault' error at run-time.  I know the
> 'istream' is being opened correctly because reading into a 'String'
> object with >> works fine.  Am I doing something wrong ?
> The sample program and "data" illustrate the point:
> 
> main()
> {
>     istream from("data",io_readonly,a_useonly);
>     char *line;
> 
>     from.get(line,15);
>     cout << line;
> }
> 
> Sample "data" file contents:
> word1 word2
> word3
> 

There are 3 istream functions for reading char*'s 

  istream&      get    (char* s, int n, char terminator = '\n');
  istream&      getline(char* s, int n, char terminator = '\n');
  istream&      gets   (char **s, char terminator = '\n');
 
The first two *require* an allocated char* (they differ only
in how the trailing terminator is handled.) To use them, you
must supply either a char[N] or an allocated char*.
The third automatically allocates space for you, that you should
later delete.

For example,

main()
{
    istream from("data",io_readonly,a_useonly);
    char *line = new char[16]; // enough for string + null
    char line2[16];
    char* line3 = 0; // `= 0' so delete'able even if never allocated --
                     // Not necessary in this example.

    from.get(line,15);
    from.get(line2,15);
    from.gets(&line3,15);  // pass in the addr of line3

    cout << line;
    cout << line2;
    cout << line3;

    delete line;
    delete line3;
}

Using the String class is a very good way to avoid dealing with these
kinds of char* allocation and semantics issues.

-Doug
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads