_expand Functions

Description

Change the size of a memory block.

#include <malloc.h> Required only for function declarationsvoid *_expand( void *memblock, size_t size );

void __based( void ) *_bexpand( __segment seg,
void __based( void ) *memblock, size_t size );

void __far *_fexpand( void __far *memblock, size_t size );

void __near *_nexpand( void __near *memblock, size_t size );

 
memblock Pointer to previously allocated memory block  
size New size in bytes  
seg Value of base segment  

Remarks

The _expand family of functions changes the size of a previously allocated memory block by attempting to expand or contract the block without moving its location in the heap. The memblock argument points to the beginning of the block. The size argument 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.

The memblock argument 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 one of the _expand functions.

The seg argument is the segment address of the __based heap.

In large data models (compact-, large-, and huge-model programs), _expand maps to _fexpand. In small data models (tiny-, small-, and medium-model programs), _expand maps to _nexpand.

The various _expand functions change the size of the storage block in the data segments shown in the list below:

Function Data Segment

_expand Depends on data model of program
_bexpand Based heap specified by seg, or in all based heaps if seg is zero
_fexpand Far heap (outside default data segment)
_nexpand Near heap (inside default data segment)

Return Value

The _expand family of functions returns a void pointer to the reallocated memory block. Unlike realloc, _expand cannot move a block to change its size. This means the memblock argument to _expand is the same as the return value if there is sufficient memory available to expand the block without moving it.

With the exception of the _bexpand function, these functions return NULL if there is insufficient memory available to expand the block to the given size without moving it. The _bexpand function returns _NULLOFF if insufficient memory is available. The item pointed to by memblock will have been expanded as much as possible in its current location.

The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. The new size of the item can be checked with one of the _msize functions. To get a pointer to a type other than void, use a type cast on the return value.

Compatibility

_expand

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_bexpand, _fexpand, _nexpand

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:None

See Also

calloc functions, free functions, malloc functions, _msize functions, realloc functions

Example

/* EXPAND.C */

#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

void 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 __far *)bufchar );

if( (bufchar = (char *)_expand( bufchar, 1024 )) == NULL )

printf( "Can't expand" );

else

printf( "Expanded block to %d bytes at %Fp\n",

_msize( bufchar ), (void __far *)bufchar );

/* Free memory */

free( bufchar );

exit( 0 );

}

Output

Allocate a 512 element buffer

Allocated 512 bytes at 0067:142A

Expanded block to 1024 bytes at 0067:142A