Thread: Weird segfault

  1. #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.

  2. #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.

  3. #3
    Registered User Driehoek's Avatar
    Join Date
    Oct 2009
    Posts
    4
    I'm double freeing a pointer somewhere, that must be the problem.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    yeah.. same like me, malloc doesn't identified in my compiler.. (I use visual C)
    is there someone can explain me?

  6. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread