-
Weird segfault
Hello everybody,
There is a nasty segfault in my code that keeps pestering me. The error is probably buried deeply in my beastly sized code so I won't post it and ask you to deal with it, but I just want to know when this simple code...
Code:
int* value_ptr = (int*) malloc (sizeof (int));
...can result in a segmentation fault so I can track down the error better.
My idea would be never, but still it does.
(Oh, and yes apart from 'return 0' in main, this is the last line of my code.)
Any help would be appreciated.
Output of gdb:
Code:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b011e6 in _int_malloc () from /lib/libc.so.6
-
Upon further investigation, all calls to malloc segfault. I did something weird somewhere.
-
I'm double freeing a pointer somewhere, that must be the problem.
-
If it supports your platform, give Valgrind a try. It can catch memory problems very early, so if you overwrite malloc()'s internal housekeeping, double free, etc, it will tell you right then and there. You don't have to wait till the problem manifests itself during a later call to malloc() leaving you to guess where the corruption happened.
-
yeah.. same like me, malloc doesn't identified in my compiler.. (I use visual C)
is there someone can explain me?
-
Thanks for advising Valgrind too me Cas. I forgot about it, I solved my problem with it.