LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-23-2010, 01:37 PM   #1
McCoder
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Rep: Reputation: 0
Segmentation Fault and GDB


Hello Everyone,

When I compile my C program on SUSE, I receive a segmentation fault error. To my knowledge, this occurs when a program tries to recall memory but is not allowed to.

So I'm using GDB for the first time to solve this problem. After a series of commands, I reach this point:
Code:
#0  0x00002b13e1bab127 in getdelim () from /lib64/libc.so.6
#1  0x000000000040167b in main () at dmsp_cusp.c:158
Here's the offending code in dmsp_cusp.c:
Code:
 157     printf("enter the input file name:");
 158     getline(in_fname);   
 159     fp3 = fopen(in_fname, "r"); 
 160     if(fp3 == NULL) {
 161       printf("can't open input file %s\n",in_fname);
 162       exit(1);
          }
Can someone give me more insight on segmentation faults and GDB so I can fix this problem? Thanks!
 
Old 07-23-2010, 02:30 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by McCoder View Post
Code:
 158     getline(in_fname);
What version of getline is that supposed to be?

So far as I can infer from other details in your post, it is the one documented here:

http://www.gnu.org/s/libc/manual/htm...ine-Input.html

But if that is it, then you are calling it incorrectly.

First notice that it takes three parameters, not one.

Also notice that the first parameter is not the result variable (as you seem to have passed). C doesn't support "call by reference", so a parameter can't be the result variable. The first parameter is the address of the result variable.

Quote:
Can someone give me more insight on segmentation faults and GDB so I can fix this problem?
That would be a lot to teach. I don't think you're ready.

It should be obvious that the info you got from GDB tells you something went wrong inside a function called from line 158 of your dmsp_cusp.c.

An expert using GDB could figure out quite a lot about what went wrong and why.

But even an expert would first look at line 158 and see if anything is obviously wrong with it.

Since line 158 is obviously wrong (as I described above) there is no reason to try any more advanced use of GDB to dig out more details.

Last edited by johnsfine; 07-23-2010 at 02:44 PM.
 
  


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
gftp Segmentation fault need help with gdb j0hn-d0e Linux - Software 4 11-05-2009 02:14 AM
gdb itself Segmentation faults johnsfine Linux - Software 2 09-08-2008 01:53 PM
gdb segmentation fault i.you Linux - Software 1 04-11-2007 04:25 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation Fault XPediTioN Slackware 2 09-18-2003 08:16 AM

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

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

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