Name
_expand - Changes the size of a previously allocated memory
block.
Syntax
#include <malloc.h>
void *_expand(block, size)
void *block;
block (_expand);
size_t size;
Description
The _expand function 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 block
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 block 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 block points to a
freed block, the block remains free after the call to
_expand .
Return Value
The _expand function returns a void pointer to the
reallocated memory block. Unlike realloc, _expand cannot
move a block to change its size. This means the block
argument to _expand is the same as the return value if there
is sufficient memory available to expand the block without
moving it.
The return value is NULL if there is insufficient memory
available to expand the block to the given size without
moving it. In this case, the item block points to 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 the
_msize function. To get a pointer to a type other than void,
use a type cast on the return value.
See Also
calloc(S), free(DOS), malloc(DOS), _msize(DOS), realloc(S)
Example
#include <stdio.h> #include <malloc.h>
main()
{
long *oldptr;
size_t newsize = 64000;
/* Get original memory: */
oldptr = (long *)malloc(10000*sizeof(long));
printf("Size of memory block pointed to by ");
printf("oldptr = %u\n", _msize(oldptr));
/* Test whether _expand succeeded: */
if (_expand(oldptr,newsize) != NULL)
printf("Expand was able to increase");
printf("block to %u\n", _msize(oldptr));
else
/* Otherwise _expand failed: */
printf("Expand was able to increase block
");
printf("to only %u\n", _msize(oldptr));
}
Sample output:
Size of memory block pointed to by oldptr = 40000
Expand was able to increase block to only 44718
This program allocates a block of memory for oldptr and uses
_msize to display the size of that block. Next, it uses
_expand to expand the amount of memory used by oldptr.
Finally, it calls _msize again to display the new amount of
memory allocated to oldptr.
(printed 6/18/89)