free() is killing my program?

This is a discussion on free() is killing my program? within the C Programming forums, part of the General Programming Boards category; So I'm writing a program and for some reason if I leave out the free part it doesn't work in ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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
    Katy, Texas
    Posts
    2,309
    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,211
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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,211
    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
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,211
    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:

    Whaddya mean I clobbered past the mem block?

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, 01: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, 01: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

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