LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Short C++ program segmentation fault. Using GCC 4.4.5 on Ubuntu 32bit (https://www.linuxquestions.org/questions/programming-9/short-c-program-segmentation-fault-using-gcc-4-4-5-on-ubuntu-32bit-885916/)

just1alexx 06-12-2011 11:58 AM

Short C++ program segmentation fault. Using GCC 4.4.5 on Ubuntu 32bit
 
Hi... I'm very excited to post a question, since I always do everything by myself.

I'm reading several lines from a text file using istream::getline(char *, streamsize n). I get the file name from the command line. Here is the code:

Code:

#include <iostream>
#include <fstream>

using namespace std;

int main ( int argc, char *argv[])
{
    char *line = new char;
    ifstream in (*++argv);       
   
    if ( in.is_open() )
    {     
        while ( in.good() )
        {
            in.getline (line, 1000);           
            cout << "line:\n" << line << "\n\n";
        }         
           
        in.close();     
    }   
   
    return 0;
}

Then I get segmentation fault right when at the end of the file.
Strange thing is that i swap the lines:
char *line = new char;
ifstream in (*++argv);
And everything works fine.

Using GCC 4.4.5 on Ubuntu 32bit

Btw: I know assembly language and know what a segmentation fault is

paulsm4 06-12-2011 12:01 PM

Hi -

You didn't actually allocate any space for your lines. You just allocated one character; you need a character array:
Code:

  const int MAX_LINE = 1000;
  char *line = new char[MAX_LINE];
  ...
  in.getline (line, MAX_LINE - 1);

Each line was overwriting memory, but in this case, the problem didn't actually manifest itself until you tried to "close()" the (now corrupt) file pointer.

PS:
If this is a text file, you might be better off using a C++ "string" type, instead of a low level C "char[]" array.

just1alexx 06-12-2011 12:16 PM

Thanks so much for your help, I did not expect such a fast reply.
You are right, I did not consider the fact that I was overwriting memory because I had allocated only one char.
Also, why do you say that I'm better off with a string class? Is it because it offers better functionality or because it's more reliable?

dwhitney67 06-12-2011 12:17 PM

Quote:

Originally Posted by paulsm4 (Post 4383454)
If this is a text file, you might be better off using a C++ "string" type, instead of a low level C "char[]" array.

I agree. A simpler program would look like:
Code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ( int argc, char *argv[])
{
    string line;
    ifstream in (argv[1]);  // argv[1] is clearer to read than *++argv
   
    if ( in )  // the stream object is converted to bool
    {     
        while ( getline(in, line) )  // getline() returns the stream, which in turn is converted to bool
        {         
            cout << "line:\n" << line << "\n" << endl;  // use endl to ensure the output stream is flushed
        }         
           
        in.close();  // superfluous; file would be closed automatically when handle falls out of scope
    }   
   
    return 0;
}


just1alexx 06-12-2011 12:25 PM

Thanks!!, much to improve my programming!!


All times are GMT -5. The time now is 05:12 PM.