Thread: free() is killing my program?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    15

    free() is killing my program?

    So I'm writing a program and for some reason if I leave out the free part it doesn't work in random places so I was wondering if I did something wrong.

    Code:
    int* list = (int*)malloc(0);
    int num = 0;
    
    for(i=0; i < something; i++)
    {
        if(something random happens)
        {
             num++;
             list = (int*) realloc(list, sizeof(int*) * num);
             list[num - 1] = something;
        }
    }
    
    free(list);
    When I leave out the free part the program runs and finishes fine, but when I leave it in, it segfaults at a random and place. What actually happens is, another unrelated pointer is modified and when its accessed I get the error, am I doing something wrong?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That usually means you've done something bad in the rest of your program.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Is this valid?

    Code:
    int* list = (int*)malloc(0);
    If so, where does it point?

    Todd

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    AFAIK, you can call realloc with a NULL pointer, and it will behave like malloc().... I think.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Calling realloc with NULL is valid:
    Quote Originally Posted by the man page
    If ptr is NULL, realloc() behaves like malloc() for the specified size.
    This, however, is still questionable:
    Code:
    list = (int*) realloc(list, sizeof(int*) * num);
    1) You should probably be allocating space for integers, and not integer pointers.
    2) You're not checking the return value from realloc() - are you _sure_ it is succeeding at each call during your program's execution?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    AFAIK, you can call realloc with a NULL pointer, and it will behave like malloc().... I think.
    Supposed to, but can't be trusted to work everywhere.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Todd Burch View Post
    Is this valid?

    Code:
    int* list = (int*)malloc(0);
    If so, where does it point?

    Todd
    Apparently it's valid and returns a pointer to a zero length array...
    Quote Originally Posted by MSDN
    If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by alwut View Post
    So I'm writing a program and for some reason if I leave out the free part it doesn't work in random places so I was wondering if I did something wrong.

    Code:
    int* list = (int*)malloc(0);
    int num = 0;
    
    for(i=0; i < something; i++)
    {
        if(something random happens)
        {
             num++;
             list = (int*) realloc(list, sizeof(int*) * num);
             list[num - 1] = something;
        }
    }
    
    free(list);
    When I leave out the free part the program runs and finishes fine, but when I leave it in, it segfaults at a random and place. What actually happens is, another unrelated pointer is modified and when its accessed I get the error, am I doing something wrong?
    Other than that fact you're casting malloc() & realloc() (see the FAQ), I don't see anything wrong in this piece of code.
    However, I would strongly suggest setting list to NULL after you free it. That way you can easily check whether it's pointing to a valid pointer by comparing it to NULL.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And the fact that you would actually malloc 0 bytes - I'm thinking that's kindof undefined behavior.
    And if realloc fails, all the memory is lost and free will free an invalid pointer, probably NULL.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    Supposed to, but can't be trusted to work everywhere.
    Really? Under what conditions would it not work as specified?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In fact, the behaviour is standard. From C99 section 7.20.3.4:
    "If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size."

    So for those compilers for which it cannot be trusted to work, those compilers themselves cannot be trusted to conform to the standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    BTW, with regard to malloc()ing a 0 byte block of memory, the results are implementation-defined according to this:

    http://c-faq.com/ansi/malloc0.html

    Apparently, it will either return a pointer that points to a block of 0 bytes, or return NULL. If it was my choice for my implementation, I believe I would return NULL, since I can't think of a real usage for a block of 0 bytes.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    malloc(0) was recently discussed here:

    http://cboard.cprogramming.com/showt...t=96232&page=2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need a program to access a program..
    By willc0de4food in forum Windows Programming
    Replies: 0
    Last Post: 03-23-2006, 02:49 AM
  2. can't free() and program terminate.
    By Mathsniper in forum C Programming
    Replies: 1
    Last Post: 06-10-2005, 02:33 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Making a program to free up Ram memory
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2004, 06:52 AM