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.
|
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! ( : |
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 ... |
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 |
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. |
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*. |
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. |
Quote:
char *pointer=malloc(sizeof(char)*250); scanf(" %c ", pointer); |
Got it fixed, was just missing an asteric on one of my lines. Thanks!
|
Quote:
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. |
I have to go out of town for a day I am going to mess with it when I get back.
|
Quote:
|
Quote:
|
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 |
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. |