_expand

From RAD Studio
Jump to: navigation, search

Go Up to malloc.h Index


Header File

malloc.h

Prototype

void *_expand(void *block, size_t size);

Description

Grows or shrinks a heap block in place.

This function attempts to change the size of an allocated memory block without moving the block's location in the heap. The data in the block are not changed, up to the smaller of the old and new sizes of the block. The block must have been allocated earlier with malloc, calloc, or realloc, and must not have been freed.

Return Value

If _expand is able to resize the block without moving it, _expand returns a pointer to the block, whose address is unchanged. If _expand is unsuccessful, it returns a NULL pointer and does not modify or resize the block.

Portability

POSIX ANSI C ANSI C++ Win32 Win64 macOS
_expand +

Example

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

void main(void) {
    char *bufchar, *newbuf;
    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 ((newbuf = (char *) _expand(bufchar, 1024)) == NULL)
        printf("cannot expand");
    else {
        bufchar = newbuf;
        printf(" Expanded block to %d bytes at %Fp\n", _msize(bufchar),
            (void __far *)bufchar);
    }
    free(bufchar);
    exit(0);
}