LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2011, 11:58 AM   #1
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Rep: Reputation: Disabled
Smile 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
 
Old 06-12-2011, 12:01 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.

Last edited by paulsm4; 06-12-2011 at 12:05 PM.
 
1 members found this post helpful.
Old 06-12-2011, 12:16 PM   #3
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-12-2011, 12:17 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by paulsm4 View Post
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;
}
 
1 members found this post helpful.
Old 06-12-2011, 12:25 PM   #5
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks!!, much to improve my programming!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SEGMENTATION FAULT using gcc 4.4.4 -O2 , works with gcc 4.1.0 -O2 or gcc 4.4.4 -O1 amir1981 Programming 36 07-26-2010 06:07 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
Segmentation Fault and Gcc compiler skarland Linux - Newbie 2 02-14-2007 09:52 AM
compiling in gcc goes well, gives segmentation fault jshine Programming 6 12-19-2004 01:08 AM
gcc / segmentation fault skeletal29 Linux - Software 0 05-05-2002 04:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration