Thread: segmentation fault (calloc, free)

  1. #1
    Registered User
    Join Date
    Jun 2007

    segmentation fault (calloc, free)


    I've found and fixed a segmentation fault, but was wondering if anyone can explain why it happened in the first place.

    At one point i freed some memory that was no longer needed (i'm 99.9 % sure of this). When the next call to calloc happened, bang!


  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    London, UK
    Perhaps you tried to free the same part of memory twice?

    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > When the next call to calloc happened, bang!
    You've trashed memory is the most likely cause.

    Around each block of memory allocated, the system typically maintains additional information to manage the overall memory pool, and to allow things like free() to work without you also having to specify the size of the memory to be freed.

    If you're sloppy with the use of the memory (array oversteps, buffer overruns), you corrupt this hidden (and vital) information, and BANG, your program is dead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM
  5. segmentation fault
    By incognito54 in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:04 PM