IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ticket #801: memory_bug.c

File memory_bug.c, 653 bytes (added by Paul Price, 20 years ago)

Program to reproduce the bug in psRealloc()

Line 
1#include <stdio.h>
2#include <unistd.h>
3#include <pslib.h>
4
5
6#define NUM_ALLOCS 10000
7
8#define ALLOC_SIZE 10
9#define REALLOC_SIZE 100
10
11void ps_memory(void)
12{
13 void *buffer = psAlloc(ALLOC_SIZE);
14 buffer = psRealloc(buffer, REALLOC_SIZE);
15 psFree(buffer);
16}
17
18void gcc_memory(void)
19{
20 void *buffer = malloc(ALLOC_SIZE);
21 buffer = realloc(buffer, REALLOC_SIZE);
22 free(buffer);
23}
24
25
26int main(int argc, char *argv[])
27{
28 psLibInit(NULL);
29
30 for (int i = 0; i < NUM_ALLOCS; i++) {
31 ps_memory();
32 printf("sbrk(): %zd\n", sbrk(0));
33 }
34
35 printf("sbrk(): %zd\n", sbrk(0));
36
37 psLibFinalize();
38
39 return EXIT_SUCCESS;
40}