LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C, segmentation fault (https://www.linuxquestions.org/questions/programming-9/c-segmentation-fault-156985/)

mattp 03-12-2004 11:45 PM

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.

mattp 03-12-2004 11:56 PM

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! ( :

naren 03-13-2004 12:14 AM

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 ...

naren 03-13-2004 12:24 AM

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

cjcuk 03-13-2004 05:02 AM

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.

Hko 03-13-2004 05:09 AM

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*.

krajzega 03-13-2004 05:20 AM

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.

haobaba1 03-13-2004 12:39 PM

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);

mattp 03-13-2004 05:31 PM

Got it fixed, was just missing an asteric on one of my lines. Thanks!

krajzega 03-14-2004 06:27 AM

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.

haobaba1 03-14-2004 10:26 AM

I have to go out of town for a day I am going to mess with it when I get back.

vasudevadas 03-14-2004 01:40 PM

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. :D

krajzega 03-14-2004 03:47 PM

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. :D
Lol ;-)

bigearsbilly 03-16-2004 05:45 AM

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

sasa 03-22-2004 09:14 PM

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


All times are GMT -5. The time now is 12:10 AM.