File: error_counts.c

package info (click to toggle)
valgrind 1%3A3.3.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 34,452 kB
  • ctags: 27,778
  • sloc: ansic: 234,398; sh: 14,186; xml: 11,662; perl: 4,410; asm: 3,135; makefile: 3,011; exp: 625; cpp: 255; haskell: 195
file content (57 lines) | stat: -rw-r--r-- 1,367 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "../memcheck.h"

int main(void)
{
   int  x;
   int  y = 0;
   int* reachable;
   int* dubious;
   int* leaked;
   long n_reachable  = 0;
   long n_dubious    = 0;
   long n_leaked     = 0;
   long n_suppressed = 0;

   /* we require these longs to have same size as a machine word */
   assert(sizeof(long) == sizeof(void*));

   /* Error counting */
   printf("errors: %d\n", VALGRIND_COUNT_ERRORS);

   if (x == 0) {
      y++;
   } else {
      y--;
   }

   printf("errors: %d\n", VALGRIND_COUNT_ERRORS);

   /* Leak checking */
   VALGRIND_DO_LEAK_CHECK;
   VALGRIND_COUNT_LEAKS(n_leaked, n_dubious, n_reachable, n_suppressed);
   if (n_reachable == 24) n_reachable = 0; /* handle glibc differences */
   printf("leaks: %ldB, %ldB, %ldB, %ldB\n",
          n_leaked, n_dubious, n_reachable, n_suppressed);

   leaked = malloc(77);
   leaked = 0;

   dubious = malloc(88);
   dubious += 10;

   reachable = malloc(99);

   VALGRIND_DO_LEAK_CHECK;
   VALGRIND_DO_LEAK_CHECK;
   VALGRIND_COUNT_LEAKS(n_leaked, n_dubious, n_reachable, n_suppressed);
   if (n_reachable == 123) n_reachable = 99; /* handle glibc differences */
   printf("leaks: %ldB, %ldB, %ldB, %ldB\n",
          n_leaked, n_dubious, n_reachable, n_suppressed);

   printf("errors: %d\n", VALGRIND_COUNT_ERRORS);

   return 0;
}