1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {

    // MEMORY LEAK -- messier
    int* a = malloc(sizeof(*a) * 3); // allocates 3 * 4 = 12 bytes
    int* b = malloc(sizeof(*a) * 4); // allocates 4 * 4 = 16 bytes
    int* c = malloc(sizeof(*a) * 5); // ..
    int* d = malloc(sizeof(*a) * 6); // ..
    int* e = malloc(sizeof(*a) * 7); // allocates 7 * 4 = 28 bytes
                                     // total = 100 bytes
                                     // assuming sizeof(int) is 4 bytes
    // Free only 2 of them (b and c)
    free(b);
    free(c);
    return EXIT_SUCCESS;
}
/*
HEAP SUMMARY:
    in use at exit: 64 bytes in 3 blocks
  total heap usage: 5 allocs, 2 frees, 100 bytes allocated

12 bytes in 1 blocks are definitely lost in loss record 1 of 3
   at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
   by 0x4005BE: main (leak_fancy.c:7)

24 bytes in 1 blocks are definitely lost in loss record 2 of 3
   at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
   by 0x4005E8: main (leak_fancy.c:10)

28 bytes in 1 blocks are definitely lost in loss record 3 of 3
   at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
   by 0x4005F6: main (leak_fancy.c:11)

LEAK SUMMARY:
   definitely lost: 64 bytes in 3 blocks
   indirectly lost: 0 bytes in 0 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 0 bytes in 0 blocks
        suppressed: 0 bytes in 0 blocks
*/
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2017 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.