Thread: Using realloc to make memory smaller?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Red face Using realloc to make memory smaller?

    Suppose you've allocated a certain number of bytes but later find that you want to add values to it. You could copy everything into a larger memory area, which is inefficient, or you can allocate more bytes using realloc, without losing your data. So far so good...

    But does this also means that I can make the memory area smaller? In my app I allocate a BIG area of memory (to be safe) because I don't know how much I need. After this area is used I want to release the memory I don't need. Is that possible with realloc?

    Example: My memory area is 10000 bytes. I only use for example 1000 bytes. I want to release 9000 bytes.
    You cantīt teach an old dog new tricks.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Absolutely you can.

    A better idea though, perhaps is just to keep up with the larger chunk once it reaches that size. Just double the size to grow it.

    It is of course possible to do your own memory management off a single buffer which is nice since you can run things your own way. It's interesting how fun this actually can be. No doubt a good way to experience what it must be like to write a memory manager for an operating system or something. My program featured little self-destructive pointers too that when nulled
    otherwise altered would deallocate the memory automatically:

    void track(void** a)
    store address 'a'
    store pointer to data 'd' (*a)
    if(*a != d ) deallocate

    basically. if you call realloc a lot you might fragment your memory space so much that it wouldn't even be worth it. Just reuse large buffers if necessary.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    10

    But does this also means that I can make the memory area smaller? In my app I allocate a BIG area of memory (to be safe) because I don't know how much I need. After this area is used I want to release the memory I don't need. Is that possible with realloc?
    Yes ,Realloc can be used to make memory area smaller.

    But Realloc dosen't free the memory that was allocated earlier.
    Memory can only be freed using free.
    "There is no alternative to consitency..."

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by gotyatech
    Yes ,Realloc can be used to make memory area smaller.

    But Realloc dosen't free the memory that was allocated earlier.
    Memory can only be freed using free.
    But what's the point then to use realloc? Must I use both realloc and free?

    Maybe it's better to allocate a BIG area, and after it's used I move the bytes to a new area that is exactly the size I want or?
    You cantīt teach an old dog new tricks.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    Ya ur point seems to be vaild.

    realloc also does free....i checked it.

    check the following link u can find source code for realloc

    http://www.lua.org/source/src_lmem.c.html
    "There is no alternative to consitency..."

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    A common mistake using realloc is that people think realloc always free the old memory block. This is not always true:
    Code:
    char *t;
    
    if((t = malloc(1024)) == NULL)
    {
       /* error handling */
    }
    
    if((t = realloc(t, 1024 * 2)) == NULL)
    {
       /* damn, realloc failed but old memory chunk is still allocated */
       /* and I have nothing pointing to it -> memory leak */
    }
    Better to do it this way:
    Code:
    char *t;
    char *tmp;
    
    if((t = malloc(1024)) == NULL)
    {
       /* error handling */
    }
    
    if((tmp = realloc(t, 1024 * 2)) == NULL)
    {
       /* t is still pointing to 1024 bytes allocated memory */
    }else
    {
       /* t is now pointing to nothing and tmp to 2048 bytes allocated memory */
    }

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Memory can only be freed using free.
    Not true, realloc() can be used in place of both malloc() and free(). If you pass a null pointer and a nonzero size then it acts like malloc, if you pass a nonnull pointer and a zero size then it frees, otherwise it resizes the block you pass it according to the new size and maybe relocates to a new block. :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM