Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>realloc() reallocate memory block</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 realloc()               Reallocate Memory Block

 #include   <malloc.h>                   Required for declarations only
 #include   <stdlib.h>                   For ANSI compatibility

 void       *realloc(ptr,size);
 void       *ptr;                        Pointer to allocated memory block
 size_t size;                            New size in bytes

    realloc() adjusts the size of a memory block that was allocated
    previously. 'ptr' is the address of the block being resized, and
    'size' is the new size in bytes.  The contents of the block, up to
    the shorter of the new and old sizes, are unaffected.  If 'ptr' is
    NULL, a new block of 'size' bytes is allocated.

    Returns:    Pointer to the reallocated block.  If the block cannot be
                reallocated, then NULL is returned and the original block
                is freed.

      Notes:    'ptr' can point to a freed block of memory, as long as
                none of the following has been called since the block was
                freed:  calloc(), malloc(), halloc(), realloc(), or
                _expand().

                _expand() can be used to change the size of a block
                without moving it.

 Compatibility: MSC version 4.0 allowed malloc() to allocate a zero-
                length item in the heap if 'size' == 0, then the
                resulting pointer could be used by realloc() to adjust
                the size at any time.

   -------------------------------- Example ---------------------------------

    The following statements allocate 500 bytes and then reallocate the
    block to 1000 bytes; if the reallocation fails, the original memory
    block is reallocated to the original size:

           #include <malloc.h>
           #include <stdio.h>     /* for printf and NULL */

           char *memptr, *temp;

           main()
           {
               if ((memptr = malloc(500)) == NULL)
                   printf("not enough room to allocate memory\n");
               else {
                   .
                   /* assign values in '*memptr' block */
                   .
                   if ((temp = realloc(memptr, 1000)) == NULL) {
                       printf("not enough room to reallocate memory\n");
                       memptr = realloc(memptr, _msize(memptr));
                    }
                   else
                       memptr = temp;
                   /* assigned values in '*memptr' block are still valid */
               }
           }


See Also: calloc() free() halloc() malloc() _msize()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson