Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo 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   <alloc.h>                    Required for declarations only
 #include  <stdlib.h>

 void       *realloc(ptr,size);
 void       *ptr;                        Pointer to allocated memory block
 size_t 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. If 'size' == 0, NULL is returned.

   -------------------------------- 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 <alloc.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, sizeof(memptr));
                   }
                   else
                      memptr = temp;
                /* assigned values in '*memptr' block are still valid */
               }
           }


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

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