1 | #include <stdlib.h>
|
---|
2 | #include <stdio.h>
|
---|
3 |
|
---|
4 | struct Block
|
---|
5 | {
|
---|
6 | Block *next;
|
---|
7 | size_t size;
|
---|
8 | };
|
---|
9 |
|
---|
10 | int 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 | /*
|
---|
30 | gcc -ggdb MemAllocFree.cpp -o MemAllocFree
|
---|
31 | MemAllocFree
|
---|
32 | */
|
---|