• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "../memcheck.h"
4 #include "leak.h"
5 
6 char *b10;
7 char *b21;
8 char *b32_33[2];
breakme()9 static void breakme() {};
f(void)10 void f(void)
11 {
12    int i;
13 
14    b10 = malloc (10);
15 
16    fprintf(stderr, "expecting details 10 bytes reachable\n"); fflush(stderr); breakme();
17    VALGRIND_DO_LEAK_CHECK;
18 
19    fprintf(stderr, "expecting to have NO details\n"); fflush(stderr); breakme();
20    VALGRIND_DO_ADDED_LEAK_CHECK;
21 
22    b10--; // lose b10
23    b21 = malloc (21);
24    fprintf(stderr, "expecting details +10 bytes lost, +21 bytes reachable\n"); fflush(stderr); breakme();
25    VALGRIND_DO_ADDED_LEAK_CHECK;
26 
27    for (i = 0; i < 2; i ++)
28       b32_33[i] = malloc (32+i);
29    fprintf(stderr, "expecting details +65 bytes reachable\n"); fflush(stderr); breakme();
30    VALGRIND_DO_ADDED_LEAK_CHECK;
31 
32    fprintf(stderr, "expecting to have NO details\n"); fflush(stderr); breakme();
33    VALGRIND_DO_ADDED_LEAK_CHECK;
34 
35    b10++;
36    fprintf(stderr, "expecting details +10 bytes reachable\n"); fflush(stderr); breakme();
37    VALGRIND_DO_ADDED_LEAK_CHECK;
38 
39    b10--;
40    fprintf(stderr, "expecting details -10 bytes reachable, +10 bytes lost\n"); fflush(stderr); breakme();
41    VALGRIND_DO_CHANGED_LEAK_CHECK;
42 
43    b10++;
44    fprintf(stderr, "expecting details -10 bytes lost, +10 bytes reachable\n"); fflush(stderr); breakme();
45    VALGRIND_DO_CHANGED_LEAK_CHECK;
46 
47    b32_33[0]--;
48    fprintf(stderr, "expecting details 32 (+32) bytes lost, 33 (-32) bytes reachable\n"); fflush(stderr); breakme();
49    VALGRIND_DO_CHANGED_LEAK_CHECK;
50 
51    fprintf(stderr, "finished\n");
52 }
53 
main(void)54 int main(void)
55 {
56    DECLARE_LEAK_COUNTERS;
57 
58    GET_INITIAL_LEAK_COUNTS;
59 
60    f();   // see leak-cases.c
61 
62 
63    GET_FINAL_LEAK_COUNTS;
64 
65    PRINT_LEAK_COUNTS(stderr);
66 
67    return 0;
68 }
69