This documentation is archived and is not being maintained.


Changes the size of a memory block.

void *_expand( 
   void *memblock,
   size_t size 


Pointer to previously allocated memory block.
New size in bytes.

Return Value

_expand returns a void pointer to the reallocated memory block. _expand, unlike realloc, cannot move a block to change its size. Thus, if there is sufficient memory available to expand the block without moving it, the memblock parameter to _expand is the same as the return value.

_expand returns NULL when an error is detected during its operation. For example, if _expand is used to shrink a memory block, it might detect a corruption in the small block heap or an invalid block pointer and return NULL.

if there is insufficient memory available to expand the block to the given size without moving it. The item pointed to by memblock is expanded as much as possible in its current location.

The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To check the new size of the item, use _msize. To get a pointer to a type other than void, use a type cast on the return value.


The _expand function changes the size of a previously allocated memory block by trying to expand or contract the block without moving its location in the heap. The memblock parameter points to the beginning of the block. The size parameter gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes. memblock can also point to a block that has been freed, as long as there has been no intervening call to calloc, _expand, malloc, or realloc. If memblock points to a freed block, the block remains free after a call to _expand.

When the application is linked with a debug version of the C run-time libraries, _expand resolves to _expand_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.


Function Required header Compatibility
_expand <malloc.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_expand.c

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

int main( void )
   char *bufchar;
   printf( "Allocate a 512 element buffer\n" );
   if( (bufchar = (char *)calloc( 512, sizeof( char ) )) == NULL )
      exit( 1 );
   printf( "Allocated %d bytes at %Fp\n", 
         _msize( bufchar ), (void *)bufchar );
   if( (bufchar = (char *)_expand( bufchar, 1024 )) == NULL )
      printf( "Can't expand" );
      printf( "Expanded block to %d bytes at %Fp\n", 
            _msize( bufchar ), (void *)bufchar );
   /* Free memory */
   free( bufchar );
   exit( 0 );

Sample Output

Allocate a 512 element buffer
Allocated 512 bytes at 002C12BC
Expanded block to 1024 bytes at 002C12BC

See Also

Memory Allocation Routines | calloc | free | malloc | _msize | realloc | Run-Time Routines and .NET Framework Equivalents