heapchecknode

From RAD Studio
Jump to: navigation, search

Go Up to alloc.h Index


Header File

alloc.h

Prototype

int heapchecknode(void *node);

Description

Checks and verifies a single node on the heap.

If a node has been freed and heapchecknode is called with a pointer to the freed block, heapchecknode can return _BADNODE rather than the expected _FREEENTRY. This is because adjacent free blocks on the heap are merged, and the block in question no longer exists.

Return Value

One of the following values:

_BADNODE Node could not be found
_FREEENTRY Node is a free block
_HEAPCORRUPT Heap has been corrupted
_HEAPEMPTY No heap
_USEDENTRY Node is a used block

Portability

POSIX ANSI C ANSI C++ Win32 Win64 macOS
heapchecknode +

Example

#include <stdio.h>
#include <alloc.h>
#define NUM_PTRS  10
#define NUM_BYTES 16

int main(void) {
    char *array[NUM_PTRS];
    int i;
    for (i = 0; i < NUM_PTRS; i++)
        array[i] = (char *) malloc(NUM_BYTES);
    for (i = 0; i < NUM_PTRS; i += 2)
        free(array[i]);
    for (i = 0; i < NUM_PTRS; i++) {
        printf("Node %2d ", i);
        switch (heapchecknode(array[i])) {
        case _HEAPEMPTY:
            printf("No heap.\n");
            break;
        case _HEAPCORRUPT:
            printf("Heap corrupt.\n");
            break;
        case _BADNODE:
            printf("Bad node.\n");
            break;
        case _FREEENTRY:
            printf("Free entry.\n");
            break;
        case _USEDENTRY:
            printf("Used entry.\n");
            break;
        default:
            printf("Unknown return code.\n");
            break;
        }
    }
    return 0;
}