1 2 /* Basic check of variable location identification, in a zero-biased 3 executable. */ 4 5 /* Relevant compile flags are: 6 7 -Wall -g -I$prefix/include/valgrind 8 9 eg -Wall -g -I`pwd`/Inst/include/valgrind 10 */ 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <assert.h> 15 #include "memcheck/memcheck.h" 16 17 /* Cause memcheck to complain about the address "a" and so to print 18 its best guess as to what "a" actually is. a must be 19 addressible. */ 20 21 void croak ( void* aV ) 22 { 23 char* a = (char*)aV; 24 char* undefp = malloc(1); 25 char saved = *a; 26 assert(undefp); 27 *a = *undefp; 28 VALGRIND_CHECK_MEM_IS_DEFINED(a, 1); 29 *a = saved; 30 free(undefp); 31 } 32 33 #include <stdio.h> 34 35 int global_u1; 36 37 int global_i1 = 17; 38 39 char global_u2[10]; 40 41 char global_i2[10] = { 1,2,3,4,5,6,7,8,9,10 }; 42 43 44 int main ( void ) 45 { 46 int local; 47 char* onheap = malloc(3); 48 assert(onheap); 49 croak(onheap+1); 50 free(onheap); 51 52 croak( &global_u1 ); 53 croak( &global_i1 ); 54 croak( &global_u2[3] ); 55 croak( &global_i2[7] ); 56 croak( &local ); 57 return 0; 58 } 59