1 
2 /* A small demo of providing descriptions of structured types in error
3    messages. */
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 
croak(void * aV)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   (void) VALGRIND_CHECK_MEM_IS_DEFINED(a, 1);
29   *a = saved;
30   free(undefp);
31 }
32 
33 #include <stdio.h>
34 #include <string.h>
35 
36 typedef struct { short c1; char* c2[3]; } XX;
37 
38 typedef
39    struct _str { int bing; int bong; XX xyzzy[77]; }
40    Str;
41 
42 __attribute__((noinline))
blah(int x,int y)43 int blah ( int x, int y )
44 {
45   Str a[10];
46   memset(a, 0, sizeof(a));
47   croak(1 + (char*)(&a[3].xyzzy[x*y].c1));
48   croak( (char*)(&a[5].bong) );
49   croak( 1 + (char*)(&a[3].xyzzy[x*y].c2[2]) );
50   memset(a, 0, sizeof(a));
51   return a[3].xyzzy[x*y].c1;
52 }
53 
main(void)54 int main ( void )
55 {
56   printf("answer is %d\n", blah(3,7) );
57   return 0;
58 }
59