Thread: Segfault from calloc

  1. #1
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625

    Segfault from calloc

    Hi,

    I'm getting a Segmentation fault from a calloc call. This is the first time I've ever had something of the sort, and I'm curious as to why I'm getting such a fault/what could potentially be the problem/how I can fix it.

    Thanks.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The problem is in your code -- there is nothing at all wrong with calloc(). Your code has probably trashed memory before that function was called.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    That's the point I was getting at, though. I know nothing is wrong with calloc.

    But you say my code has "trashed memory".

    That is what I wish to know of. What does "trashed memory" mean, and where should I start looking as far as debugging goes ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What does "trashed memory" mean,
    All of the following can cause trouble later on
    - running off the end of any array (either end, whether malloc'ed or a real array)
    - using an uninitialised pointer (before calling malloc)
    - using a dead pointer (after calling free)
    - freeing the same pointer twice
    - freeing something which wasn't malloc'ed in the first place
    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.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    ok, but here's the thing, I've managed to pinpoint the exact line where I've been getting the segfault :

    Code:
    void * buffer = calloc(1,BLOCK_SIZ);
    BLOCK_SIZ is a long specified by the user. Now, based on what you've told me about trashed memory, I don't think this particular line is problematic. So I'm guessing the bug is somewhere else in the code. But then why am I getting a segmentation fault on that line !?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > So I'm guessing the bug is somewhere else in the code.
    Correct

    > But then why am I getting a segmentation fault on that line !?
    Code:
    char *p, *q;
    p = malloc(10);
    strcpy(p,"a string with far more than 10 characters in it");
    q = malloc(10);
    Imagine that the memory pool is broken up as follows
    P A P A P F P A P
    P is a link pointer to the next block
    A is an allocated block
    F is a free block.

    If you write past the end of an A block (see the strcpy), you're going to trash a P
    The next malloc / free routine is going to smash into that pointer, take a leap into the big unknown and promptly die with a segfault.

    The segfault is the effect, not the cause.
    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. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  2. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  3. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM