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>_expand() change size of allocated memory block</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 _expand()               Change Size of Allocated Memory Block

 #include   <malloc.h>                   Required for declarations only

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

    _expand() changes the size of a previously allocated memory block
    pointed to by 'ptr', but cannot move the block as can realloc().
    'size' is the new size, in bytes, of the memory block.  When the
    block is changed, its contents (up to the shorter of the two block
    sizes) remain unchanged.

    Returns:    'ptr' if the block was changed to 'size'.  If the block
                cannot be expanded to 'size', the block is expanded as
                much as possible, and NULL is returned.  The size of the
                block can be obtained by calling _msize(ptr).

      Notes:    'ptr' can point to a memory block that has been freed as
                long as one of the following has not been called in the
                meantime: calloc(), _expand(), halloc(), malloc(), or
                realloc().  However, the block will remain free after the
                call to _expand().

 Portability:   Not supported by ANSI standard.

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

    The following statements allocate a block of 500 bytes and then try
    to expand the block to hold 5000 bytes.

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

           main()
           {
               char *memptr;

               if ((memptr = malloc(500)) == NULL)
                   printf("unable to allocate 500 bytes\n");
               else {
                   if (_expand(memptr, 5000) == NULL)
                       printf("block only increased to %u bytes\n",
                              _msize(memptr));
                   else
                       printf("block increased to 5000 bytes\n");
                }
           }




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

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