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 - <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

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

    realloc() changes the size of a previously allocated memory block.
    'ptr' is the address of the block being resized, and 'size' is the
    new size in bytes.  The contents of the block are unchanged up to the
    shorter of the new and old sizes.

    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 block which has been freed, as long
                as neither calloc(), malloc(), halloc(), realloc(), nor
                _expand() has been called since the block was freed.

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

  -------------------------------- 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