Thread: Freeing memory question

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Freeing memory question

    Let's suppose I have a char* initialized to NULL and I use the realloc function to allocate fifty bytes for it. Now let's say I use only twenty-five bytes and I want to keep those allocated but free the rest. Is that possible?

  2. #2
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    I'm not totaly sure on this, but I think you could use realloc again. Something like

    Code:
    char *buffer = NULL;
    buffer = realloc(buffer, 50);
    if(buffer == NULL) {
        // Error
    }
    
    // Do something
    
    char *smaller = realloc(buffer, 25);
    if(smaller == NULL) {
        // Error
    }
    else {
        buffer = smaller;
    }

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Do you really need to realloc?
    cant you just determine how much you are going ot use then use malloc? There is no need to use realloc the first time because there is no space allocated to it to begin with.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >There is no need to use realloc the first time because there is no space allocated to it to begin with.
    realloc can be used as if it were malloc if the first argument is a null pointer, and it can be used as if it were free if the second argument is 0. This can make it simpler to write algorithms for dynamic growth without special cases, but it can also be confusing.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    It is not confusing, it's just a matter of reading the function's definition and understanding it.

    I'm talking like that because I think I finally get it lol.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It is not confusing, it's just a matter of reading the function's definition and understanding it.
    No, it is confusing because the function does more than one thing and all of those things are conceptually different. A function is easier to understand if it doesn't try to be three functions.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    207
    Prelude, will the use of realloc as "free" make the above example look like this:

    Code:
    char *buffer = NULL;
    buffer = realloc(buffer, 50);
    if(buffer == NULL) {
        // Error
    }
    
    // Do something
    
    buffer = realloc(buffer, 0);
    if(buffer == NULL) {
        // Error
    }
    And this leaves the data stored in *buffer intact, right?

    mw
    Last edited by Lionmane; 08-28-2005 at 10:27 AM.
    Blucast Corporation

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > And this leaves the data stored in *buffer intact, right?
    There is no data - the size you passed is 0.

    In the same way that malloc(0) may or may not return a NULL pointer, and free(ptr) doesn't affect the value stored in the pointer, you can't legally dereference any of those pointers.
    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. question about memory
    By simo_mon in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 08:02 AM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. Freeing memory
    By C_ntua in forum C Programming
    Replies: 17
    Last Post: 06-29-2008, 04:42 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. c style string memory leak question
    By curlious in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2003, 06:31 AM