ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ...
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
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.
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:
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:
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.