C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-28-2009, 06:59 PM   #1
Registered User
 
Driehoek's Avatar
 
Join Date: Oct 2009
Posts: 4
Unhappy 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

Last edited by Driehoek; 10-28-2009 at 07:11 PM. Reason: Added debuging output.
Driehoek is offline   Reply With Quote
Old 10-28-2009, 07:05 PM   #2
Registered User
 
Driehoek's Avatar
 
Join Date: Oct 2009
Posts: 4
Upon further investigation, all calls to malloc segfault. I did something weird somewhere.
Driehoek is offline   Reply With Quote
Old 10-28-2009, 07:32 PM   #3
Registered User
 
Driehoek's Avatar
 
Join Date: Oct 2009
Posts: 4
I'm double freeing a pointer somewhere, that must be the problem.
Driehoek is offline   Reply With Quote
Old 10-28-2009, 09:40 PM   #4
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
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.
cas is offline   Reply With Quote
Old 10-29-2009, 05:55 AM   #5
Registered User
 
Join Date: Oct 2009
Location: Indonesia
Posts: 60
yeah.. same like me, malloc doesn't identified in my compiler.. (I use visual C)
is there someone can explain me?
Kinshara is offline   Reply With Quote
Old 10-29-2009, 08:05 AM   #6
Registered User
 
Driehoek's Avatar
 
Join Date: Oct 2009
Posts: 4
Thanks for advising Valgrind too me Cas. I forgot about it, I solved my problem with it.
Driehoek is offline   Reply With Quote
Reply

Tags
malloc, pointer, segfault, segmentation fault

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
weird things with my linked list of queue -EquinoX- C Programming 3 11-22-2008 11:23 PM
malloc() resulting in a SegFault?! cipher82 C++ Programming 21 09-18-2008 11:24 AM
use of printf prevents segfault! MK27 C Programming 31 08-27-2008 12:38 PM
Weird fopen() segfault faffi C Programming 10 03-05-2008 12:48 PM
weird segfault in fgets in process 911help C Programming 10 12-29-2007 01:51 PM


All times are GMT -6. The time now is 12:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22