Ticket #15258: MemAllocFree.cpp

File MemAllocFree.cpp, 492 bytes (added by X512, 5 years ago)

memory free test

Line 
1#include <stdlib.h>
2#include <stdio.h>
3
4struct Block
5{
6 Block *next;
7 size_t size;
8};
9
10int main()
11{
12 size_t totalAlloc = 0, size;
13 Block *blocks = 0, *blk = 0;
14 while (1) {
15 size = 32;
16 blk = (Block*)malloc(size);
17 if (blk == 0) break;
18 blk->size = size;
19 blk->next = blocks; blocks = blk;
20 totalAlloc += size;
21 }
22 while (blocks != 0) {
23 blk = blocks; blocks = blocks->next;
24 free(blk);
25 }
26 printf("%d\n", totalAlloc);
27}
28
29/*
30gcc -ggdb MemAllocFree.cpp -o MemAllocFree
31MemAllocFree
32*/