heapwalk

From RAD Studio
Jump to: navigation, search

Go Up to alloc.h Index


Header File

alloc.h

Prototype

int heapwalk(struct heapinfo *hi);

Description

heapwalk is used to "walk" through the heap, node by node.

heapwalk assumes the heap is correct. Use heapcheck to verify the heap before using heapwalk. _HEAPOK is returned with the last block on the heap. _HEAPEND will be returned on the next call to heapwalk.

heapwalk receives a pointer to a structure of type heapinfo (declared in alloc.h). For the first call to heapwalk, set the hi.ptr field to null. heapwalk returns with hi.ptr containing the address of the first block. hi.size holds the size of the block in bytes. hi.in_use is a flag that's set if the block is currently in use.

Return Value

One of the following values:

_HEAPEMPTY No heap exists
_HEAPEND The end of the heap has been reached
_HEAPOK The heapinfo block contains valid information about the next heap block

Portability

POSIX ANSI C ANSI C++ Win32 Win64 macOS
heapwalk +

Example

#include <stdio.h>
#include <alloc.h>

#define NUM_PTRS  10
#define NUM_BYTES 16

int main(void) {
    struct heapinfo hi;
    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]);

    hi.ptr = NULL;
    printf("   Size   Status\n");
    printf("   ----   ------\n");
    while (heapwalk(&hi) == _HEAPOK)
        printf("%7u    %s\n", hi.size, hi.in_use ? "used" : "free");

    return 0;
}