LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-12-2004, 11:45 PM   #1
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Rep: Reputation: 30
C, segmentation fault


In C, what is a segmentation fault? I am writting a (very) simple program using file io and I keep getting this segmentation fault runtime error. What is it, and how can I fix it? Thanks.
 
Old 03-12-2004, 11:56 PM   #2
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
Upon further research:

Segmentation fault is caused by referencing memory thats already been freed, using pointers incorrectly, or trying to access some messed up hardware.

Im sure my error falls within the "using pointers incorrectly" as I am a novice to C. I will spend a few hours shoving in asterisks all over my code until I get frustrated and ask for help here! ( :
 
Old 03-13-2004, 12:14 AM   #3
naren
Member
 
Registered: Feb 2004
Posts: 66

Rep: Reputation: 15
segmentation fault appears as if it is not easy to understand
when u r tring to access the memory that is not allocated by u ...
 
Old 03-13-2004, 12:24 AM   #4
naren
Member
 
Registered: Feb 2004
Posts: 66

Rep: Reputation: 15
Segmentation fault appears as if it is not easy to understand
when u r tring to access the memory that is not allocated by u ...
then segmentation fault appears after executing a.out

Let me explain u with an example;

if u declare array as int a[5];
and then give printf("%d\n",a[6]);
it gives segmentation fault


How to over come this :

there r diff.. debuggers to locate the place where segmentation fault has occured ..
procedure:

if u r program is 1.c
then complie it using gcc 1.c -g
then at the command prompt type gdb a.out
u will get a prompt >>>
here type 'r'
and give the reqired input

it will give the place where u r tring to access the memory
u is not allocate by u(it will give the lines in the program 1.c here segmentation fault has occured )

u can rely on gdb about 70% and there r also other debuggers
for which u can refer to the book LInux in a nutshell
 
Old 03-13-2004, 05:02 AM   #5
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
How to cause a SIGSEGV:

1) Use an address that does not exist in the process address space in user mode.
2) Use an address that does exist in the process address space, but in a way that does match it's permissions ( eg, trying to write to a read-only page ).

The above is what causes a segmentation violation directly. This is normally the embodiment of the conditions in the first reply to the post. The most common instance of causing segmentation violations is, probably, the dereference of the NULL pointer.
 
Old 03-13-2004, 05:09 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You say your program uses file io. So I suppose you have a "FILE *myfile;" kind of declaration.
Before doing IO, you need open the file with "fopen()". Did you do that? Does you're program check if fopen() failed?

If the FILE *myfile isn't opened , or failed to open, you likely get a segfault when trying to access the FILE*.
 
Old 03-13-2004, 05:20 AM   #7
krajzega
Member
 
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92

Rep: Reputation: 15
You all are right, so I think I will say again what some of you have already said
My events when segmentation fault (memory secure abuse?) appears:
a) using not-initialized pointer as initialized or as array (for example:
char *pointer;
pointer[10]='a'
b) using pointer as a variable in scanf(), for example:
char *pointer;
scanf("%c", pointer);
should be:
char *pointer;
scanf("%c", &pointer);
c) going out of array's scope, for example:
char array[100];
array[101]='a';
d) trying to overwrite constant, for example:
char *string="Linux rulez";
string[3]='a';
e) going out of file scope or using not existing file, for example:
void function(FILE *file) {}
...
function(file.txt); //...when file doesnt exist

This is not whole list of events, but i think those are most common and popular.

Last edited by krajzega; 03-13-2004 at 05:22 AM.
 
Old 03-13-2004, 12:39 PM   #8
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
Quote:
Originally posted by krajzega

char *pointer;
scanf("%c", &pointer);
[/B]
This is very ugly, I seriously doubt that you are going to be reading in a valid memory address from the user. Don't ever do this. What you had before this would be valid had you malloced the pointer so that it pointed to some valid memory such as:

char *pointer=malloc(sizeof(char)*250);
scanf(" %c ", pointer);
 
Old 03-13-2004, 05:31 PM   #9
mattp
Member
 
Registered: Mar 2004
Location: Chicago, USA
Distribution: Slackware 10.2
Posts: 368

Original Poster
Rep: Reputation: 30
Got it fixed, was just missing an asteric on one of my lines. Thanks!
 
Old 03-14-2004, 06:27 AM   #10
krajzega
Member
 
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92

Rep: Reputation: 15
Quote:
Originally posted by haobaba1
This is very ugly, I seriously doubt that you are going to be reading in a valid memory address from the user. Don't ever do this. What you had before this would be valid had you malloced the pointer so that it pointed to some valid memory such as:

char *pointer=malloc(sizeof(char)*250);
scanf(" %c ", pointer);
Yes exactly, you are right. I've missed alocating memory, so my scanf() would try to allocate something in not-existing space, sorry.
But it doesn't change the main sense, because, this is a POINTER TO A MEMORY, not MEMORY. Scanf is special kind of function. I've already tried to compile and execute a program using your version of code, but segmentation fault also appears.

Last edited by krajzega; 03-14-2004 at 06:32 AM.
 
Old 03-14-2004, 10:26 AM   #11
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
I have to go out of town for a day I am going to mess with it when I get back.
 
Old 03-14-2004, 01:40 PM   #12
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
Quote:
Originally posted by cjcuk
How to cause a SIGSEGV:

1) Use an address that does not exist in the process address space in user mode.
2) Use an address that does exist in the process address space, but in a way that does match it's permissions ( eg, trying to write to a read-only page ).

The above is what causes a segmentation violation directly. This is normally the embodiment of the conditions in the first reply to the post. The most common instance of causing segmentation violations is, probably, the dereference of the NULL pointer.
I don't need to know how to cause a SIGSEGV. All I need to do to cause one is write a program.
 
Old 03-14-2004, 03:47 PM   #13
krajzega
Member
 
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92

Rep: Reputation: 15
Quote:
Originally posted by vasudevadas
I don't need to know how to cause a SIGSEGV. All I need to do to cause one is write a program.
Lol ;-)
 
Old 03-16-2004, 05:45 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
How to find where it happens?
Use gdb and a 'core' file to locate the offending line of code.

Firstly compile with the -g (debug) option. (export CFLAGS=-g ?)
Your program sigsev should produce a 'core' file.

(I'm doing this from memory, no gdb on my box at work so may be slightly wrong)

then you do something like:

gdb program core

when you get a prompt, type 'where' and it will take you to
the line of code that caused the problem.

easy!

read the gdb man page.
billy
 
Old 03-22-2004, 09:14 PM   #15
sasa
LQ Newbie
 
Registered: Nov 2003
Posts: 1

Rep: Reputation: 0
after i compile my prgram, there are appear the "Segmentation Fault" in the screen...then i install the LCC compiler in order to create a file such as a.out...but why cant execute it? i install the LCC-4.0-0.i386.rpm....thank u
 
  


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
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM

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

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