heapcheckfree
Go Up to alloc.h Index
Header File
alloc.h
Prototype
int heapcheckfree(unsigned int fillvalue);
Description
Checks the free blocks on the heap for a constant value.
Return Value
The return value is less then 0 for an error and greater than 0 for success. The return values and their meaning are as follows:
_BADVALUE | A value other than the fill value was found |
_HEAPCORRUPT | Heap has been corrupted |
_HEAPEMPTY | No heap |
_HEAPOK | Heap is accurate |
Portability
POSIX | ANSI C | ANSI C++ | Win32 | Win64 | OS X | |
---|---|---|---|---|---|---|
heapcheckfree | + |
Example
#include <stdio.h>
#include <alloc.h>
#include <mem.h>
#define NUM_PTRS 10
#define NUM_BYTES 16
int main(void) {
char *array[NUM_PTRS];
int i;
int res;
for (i = 0; i < NUM_PTRS; i++)
array[i] = (char *) malloc(NUM_BYTES);
for (i = 0; i < NUM_PTRS; i += 2)
free(array[i]);
if (heapfillfree(1) < 0) {
printf("Heap corrupted.\n");
return 1;
}
for (i = 1; i < NUM_PTRS; i += 2)
memset(array[i], 0, NUM_BYTES);
res = heapcheckfree(1);
if (res < 0)
switch (res) {
case _HEAPCORRUPT:
printf("Heap corrupted.\n");
return 1;
case _BADVALUE:
printf("Bad value in free space.\n");
return 1;
default:
printf("Unknown error.\n");
return 1;
}
printf("Test successful.\n");
return 0;
}