|
From: Julian B. <ju...@pa...> - 2011-11-03 13:20:28
|
On 2011-11-01, Peter Toft <pt...@li...> wrote:
> Hi all
>
> Try to find the errors in this C/C++ snippet using valgrind:
>
> #include <stdio.h>
> /* 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
|