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

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

    _expand() changes the size of the previously allocated memory block
    pointed to by 'ptr'. 'size' is the new size in bytes.  The contents
    of the block are unchanged up to the shorter of the new and old
    sizes.  The block will not be moved when expanded.

    Returns:    'ptr' if the block was expanded 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 block which has been freed as long
                as neither calloc(), _expand(), halloc(), malloc(), nor
                realloc() has been called since the block was freed.
                However, the block will remain free after the call to
                _expand().

                Unlike realloc(), _expand() cannot move a block to change
                its size.

  -------------------------------- 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: _msize() calloc() halloc() malloc() free()

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