LinuxQuestions.org
Visit Jeremy's Blog.
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 11-28-2014, 02:13 AM   #1
ISIMM
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Rep: Reputation: Disabled
segmentation fault in C


Dear all,
I wrote this program in Linux
I got segmentation fault when I run that program

So what is the problem ?
Im not able to figure out the reason for segmentation fault!!
Thanks very much for any help

Last edited by ISIMM; 11-28-2014 at 11:13 AM.
 
Old 11-28-2014, 09:54 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
There are code tags that can be used in post which will make it s/possible/easier/ to read your code.

Use stderr and write frequent output statements to isolate where the program segfaults. Remove all parts not related to the segfault and you will either be able to find your problem or have something you can post that someone can help with.

Segfaults indicate you are either using an uninitialized pointer or writing to memory that isn't reserved for your programs use.
 
1 members found this post helpful.
Old 11-28-2014, 10:09 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by SoftSprocket View Post
Use stderr and write frequent output statements to isolate where the program segfaults.
Rather use gdb which will stop at the exact point in the program where the segfault occurs.
 
Old 11-28-2014, 10:31 AM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Rather use gdb which will stop at the exact point in the program where the segfault occurs.
Well, yes, or for that matter run valgrind and likely find the problem itself. There's a number of approaches that are going to have an improved chance of success over posting a long, unformatted chunk of code.
 
Old 11-28-2014, 11:03 AM   #5
ISIMM
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
So what is the problem ?
Im not able to figure out the reason for segmentation fault!!
Thanks very much for every help[/QUOTE]
 
Old 11-28-2014, 11:05 AM   #6
ISIMM
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
So what is the problem ?
Im not able to figure out the reason for segmentation fault!!
Thanks very much for every help[/QUOTE]

Its Ok I find the problem
thanks
 
Old 11-28-2014, 12:39 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, you've deleted the code, but should you post it again, use [code] and [/code] tags.
 
Old 11-28-2014, 05:49 PM   #8
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
Quote:
I wrote this program in Linux
in "c " right ? not c++ or c#
what standard of c is it ?
c11 or an older version c99 ?
what compiler are you using ?
How are you building this unknown program ?
-- the full build line if it is a simple program (EXAMPLE: gcc Program.c -o Program -lm )

What dose this "program" do "
Are there libraries that it NEEDS that you did NOT tell gcc to use ( -lm ??? ) ?

please POST the code -- and use code tags please
-- using the PHP tags to show the std code tags
PHP Code:
 [code]
the code
line1
line2
line3
and so on 
[/code
 
Old 12-01-2014, 02:03 AM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Use stderr and write frequent output statements to isolate where the program segfaults.
Another way to detect the segmentation fault(s) might be to
search Google with the following keywords:

"detecting segmentation fault using gnu gdb"
 
Old 12-01-2014, 09:28 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
There's a variety of things you can do. If you set ulimit -c to be unlimited in your command prompt from where you run your program, it should generate a core file. Further, when you compile, presuming you're using gcc, then use the -ggdb argument to generate GDB debugging symbols and information. You can then use GDB to analyze a core file, see the backtrace, and then determine what went wrong. Or as also suggested, run from within GDB, let it break, and analyze what happened by looking at the stack and backtrace. If things get completely screwed up and you can't see what happened. Output some debug to determine how far you have gotten in your program, single step through it in the debugger, and so forth. You may want to post your code as well, but before doing so, you should try to zero in on what parts of it work and what doesn't. For instance if it's a very lengthy piece of code and it does a lot of operations before having the segmentation fault, then it will be inefficient to have people view a lot of code, they either may not or they may focus on code style or things which might be warnings versus find the location where the actual fault is.
 
Old 12-01-2014, 09:32 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by ISIMM View Post
So what is the problem ?
Im not able to figure out the reason for segmentation fault!!
Thanks very much for every help
Quote:
Originally Posted by NevemTeve View Post
Well, you've deleted the code, but should you post it again, use [code] and [/code] tags.
If you found your solution. Please do not delete the code from your original post and instead post and update to indicate what the problem was and how you fixed it. It appears you originally posted code, but deleted it. A better thing would be edit that original to post within code tags correctly. And then post an update, mark the solution as solved. This way someone else can benefit by your problem and solution. Otherwise this becomes a wasted thread which will garner some attention, but end up helping no one. In fact future searches for it will find it if someone is searching for segmentation faults, and then they'll be frustrated in not seeing any example of the solution.
 
  


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
Why segmentation fault? Superdude Programming 15 12-08-2006 03:01 AM
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 (What is that?) jlacroix Fedora 6 11-17-2003 08:32 PM
Segmentation Fault redhatnoob Linux - Software 3 11-13-2003 02:11 AM

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

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