segmentation fault (calloc, free)

This is a discussion on segmentation fault (calloc, free) within the C Programming forums, part of the General Programming Boards category; Hi, I've found and fixed a segmentation fault, but was wondering if anyone can explain why it happened in the ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    segmentation fault (calloc, free)

    Hi,

    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!

    thanks

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

    QuantumPete
    "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
    Location
    The edge of the known universe
    Posts
    32,822
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 07:32 AM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  4. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 11:29 AM
  5. segmentation fault
    By incognito54 in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 11:04 PM

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