LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-30-2006, 02:56 PM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Immediate Segmentation Fault


I'm writing an application that reads in a text file containing only the chars '0' and '1', and then writes the associated binary file. My trouble is, although my code compiles without so much as a warning, running it produces an immediate segmentation fault. (Segmentation Fault: The program recieved signal 11 SIGSEV). Debugging with printf statements (btw, it's written in C) confirms that it never even executes the first line of main().

Is it possible that such a problem is caused by faulty code (There is a lot of pointer usage which might cause a segmentation fault) if the first line is never executed? Should I look at my gcc? This gcc has served me quite well in the past, so I hesitate to suspect it, but I'm still confused.

If it's a code problem, how does one debug a program that won't even start in gdb?
 
Old 12-30-2006, 03:02 PM   #2
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
do a
[code]
gdb prog_name

then do
Code:
 
run [args]
after seg fault do bt

then post output here so we can have a look_c.
 
Old 12-30-2006, 03:15 PM   #3
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
Sounds strange. Try to isolate the problem by divide and conquer. main should look like:

Code:
int main(int argc, const char** argv) {
    ...
}
Do you use multiple files? How do you produce your binary?
 
Old 12-30-2006, 03:17 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
There are situations when the code crashes so fast, that printf is not yet shown. Check it with gdb, like one of the previous posters suggests.
 
Old 12-30-2006, 03:20 PM   #5
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
sorry i didn't see the bit where u said it will not even start in gdb.

....

is it worth trying to start it in valgrind ?
how is the file that the program reads from made?
 
Old 12-30-2006, 03:32 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
What does it mean that it doesn't start in gdb? What's the error you get (in gdb)?
 
Old 12-30-2006, 03:40 PM   #7
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Well, I feel silly, I found the error. Aparently it was just crashing so fast printf didn't register yet. It was on like the 3rd line of code. I incorrectly used strcat. I didn't realize strcat didn't just return the two arguments concatenated, it actually altered the first argument. Aparently argv[1] didn't appreciated being written to. Sorry and thanks!
 
Old 12-31-2006, 03:27 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The thing about Linux programs with standard output is that if the program crashes before the buffer is cleared then the output sitting there isn't shown. For this reason I use test output lines and immediately flush the output buffer (e.g. 'std::cout << "tag1\n"; std::cout.flush();'.) This lets me know the last test line to be executed and keeps the buffer from being erased before display.
ta0kira
 
Old 12-31-2006, 03:38 PM   #9
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
there is no such facility in straight C for that is there ??!??

cheers
 
Old 12-31-2006, 03:40 PM   #10
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
fflush
<the above must have been the only 6 characters of reply, but it's too short>
 
Old 12-31-2006, 03:45 PM   #11
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
learn something every day..
 
  


Reply



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
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault pippet Programming 4 01-24-2005 01:02 AM
Segmentation fault sin-x Slackware 2 01-12-2005 03:01 PM
Segmentation fault SocialEngineer Linux - General 0 01-07-2005 12:49 PM
segmentation fault... perdesiz Linux - Software 1 08-18-2003 01:55 AM

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

All times are GMT -5. The time now is 09:40 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