causes/fixes for Segmentation fault (core dumped)
Hi, I''m learning C under unix, I''m learning with the book "The C Programming Language", I''m getting "Segmentation fault (core dumped)" errors when I try to run my program. The book doesnt mention this error so I have no idea what it means.
Can anyone tell me what it means?
What causes it?
Any solutions for the problem?
Any strategies for tracking down the problem(without debuggers)?
Any programming practices to help avoid getting this error?
thanks everyone,
Kaveh
------------------------Kicking Software
Segmentation faults occur when your program did something that it couldnt recover from, therefore the OS dumped the program memory to a file (assuming you have it set up to do that) and quit. For my programs, segmentation faults usually mean I did something with memory that I wasnt suppose to, either going out of an array bounds or trying to use memory after I freed it etc. If you are using *nix I would suggest using gdb as it is a good way to try and establish where your program is crashing.
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
Thanks!
I found the problem, but I dont know whats wrong with it.
This is giving the problem. How can I do this correctly?
thanks again.
I found the problem, but I dont know whats wrong with it.
This is giving the problem. How can I do this correctly?
#include <stdio.h>#include <string.h>char *myString;void main(int argc, char * argv[]){ myString = strcpy(myString, "blahblah"); printf("%s\n", myString);}
thanks again.
------------------------Kicking Software
Heh, I was going to mention this in my original post about what can cause a segmentation fault. The problem is that you arent allocating any memory before you do the string copy so when you call string copy its overwriting memory that doesnt belong to the program and therefore giving you a segmentation fault. There are two ways you can fix this, you could either make a static char array or allocate memory at run time. Here are examples of both
[edited by - cmptrgear on June 25, 2003 6:28:54 PM]
//dynamic approach#include <stdio.h>#include <string.h>char * mystring;int main(int argc, char ** argv) //char ** argv is same as char * argv[]{ int length=strlen("blahblah")+1;//need +1 to inlude null terminator mystring=(char *)malloc(sizeof(char)*length); mystring=strcpy(mystring, "blahblah"); printf("%s\n", mystring); return 0;}//static approach#include <stdio.h>#include <string.h>char mystring[256]; //256 is an arbitrary sizeint main(int argc, char ** argv) //char ** argv is same as char * argv[]{ mystring=strcpy(mystring, "blahblah"); printf("%s\n", mystring); return 0;}[\source]
[edited by - cmptrgear on June 25, 2003 6:28:54 PM]
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
woohoo, now im making progress! thanks!
so is this also writing over memory i dont own?
#include <stdio.h>
#include <string.h>
char *myString;
void main(int argc, char * argv[])
{
myString = (char *) malloc(0);
myString = strcpy(myString, "blahblah");
printf("%s\n", myString);
}
also, in the dynamic approach i would have to release the memory when im done, right? free(myString); ?
sorry for the newbie questions, im used to VB and Java where everything is done automatically
so is this also writing over memory i dont own?
#include <stdio.h>
#include <string.h>
char *myString;
void main(int argc, char * argv[])
{
myString = (char *) malloc(0);
myString = strcpy(myString, "blahblah");
printf("%s\n", myString);
}
also, in the dynamic approach i would have to release the memory when im done, right? free(myString); ?
sorry for the newbie questions, im used to VB and Java where everything is done automatically
------------------------Kicking Software
quote:
Original post by kaveh
woohoo, now im making progress! thanks!
so is this also writing over memory i dont own?
#include <stdio.h>
#include <string.h>
char *myString;
void main(int argc, char * argv[])
{
myString = (char *) malloc(0);
myString = strcpy(myString, "blahblah");
printf("%s\n", myString);
}
also, in the dynamic approach i would have to release the memory when im done, right? free(myString); ?
sorry for the newbie questions, im used to VB and Java where everything is done automatically
Yup, that way you''ll be writing over memory you don''t own. The string "blahblah" has 8 characters, so you need 9 bytes to store (1 byte is for the null terminator).
Yes, if you use malloc you have free the memory later. But you don''t have to use malloc (dynamic allocation) if you don''t need it. You can allocate it just like this:
char myString[10];
This way you don''t need to free the memory later, it''ll be freed automatically at the end of the block.
Victor.
c[_]~~
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement