On Thu, 3 Nov 2011 11:09:34 +0000 (UTC), Julian Brown wrote: 

> On
2011-11-01, Peter Toft wrote:
>> Hi all Try to find the errors in this
C/C++ snippet using valgrind: #include /* Save as code.c */ int
main(void) { int i=-1,a[2],b[2],c[2]; a[0] = 1; a[1] = 2; b[0] = 3; b[1]
= 4; c[0] = 5; c[1] = 6; printf("%i %in",b[i],a[i]); return 0; } Compile
using "gcc -o bla code.c -Wall" and check the code using
> FWIW, GCC
(4.6.1) finds this bug without trouble if you use -O2: $ gcc -O2 -Wall
loss.c -o loss loss.c: In function 'main': loss.c:5:22: warning:
variable 'c' set but not used [-Wunused-but-set-variable] loss.c:9:9:
warning: array subscript is below array bounds [-Warray-bounds]
loss.c:9:9: warning: array subscript is below array bounds
[-Warray-bounds] Some warning analyses are only run at higher
optimisation levels that -O0 (the default). Jules
------------------------------------------------------------------------------
RSA(R) Conference 2012 Save 0 by Nov 18 Register now
http://p.sf.net/sfu/rsa-sfdev2dev1 [2]
_______________________________________________ Valgrind-users mailing
list [email protected] [3]
https://lists.sourceforge.net/lists/listinfo/valgrind-users [4]

small
twist to the code, then gcc -O2 -Wall finds nothing.... 

See the
comment -> run as "code -1" 

#include 
#include 

/* Save as code.c
compile "gcc -Wall -O2 o code code.c" and run as "code -1" */
int
main(int argc, char **argv)
{
 int a[2],b[2],c[2],i;
 a[0] = 1; a[1] =
2;
 b[0] = 3; b[1] = 4;
 c[0] = 5; c[1] = 6;

 printf("Dummy print ....
%in",c[0]);
 printf("argv[1] = %sn",argv[1]);
 i = atoi(argv[1]);

printf("index i = %in",i); 
 printf("%i %in",b[i],a[i]);
 return 0;
}


-- 
Peter Toft, PhD
http://petertoft.dk
  

Links:
------
[1]
mailto:[email protected]
[2] http://p.sf.net/sfu/rsa-sfdev2dev1
[3]
mailto:[email protected]
[4]
https://lists.sourceforge.net/lists/listinfo/valgrind-users
------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to