我正在学习 C 并且正在学习包含使用 valgrind 的教程。我仍在了解 valgrind 实际在做什么,并且想知道是否有人可以解释为什么它没有检测到以下代码中的任何错误:
#include <stdio.h>
int main(int argc, char *argv[])
{
int numbers[4] = {0,1,2,3,4};
// first, print them out raw
printf("numbers: %d %d %d %d %d\n",
numbers[0], numbers[1],
numbers[2], numbers[3],
numbers[4]);
return 0;
}
我确实收到编译器错误:
greggery@Lubu:~/code$ make lc
cc -Wall -g lc.c -o lc
lc.c: In function ‘main’:
lc.c:5:2: warning: excess elements in array initializer [enabled by default]
lc.c:5:2: warning: (near initialization for ‘numbers’) [enabled by default]
但是当我对 valgrind 运行它时,它没有发现任何问题:
==2300== Memcheck, a memory error detector
==2300== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2300== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2300== Command: ./lc
==2300==
numbers: 0 1 2 3 69156864
==2300==
==2300== HEAP SUMMARY:
==2300== in use at exit: 0 bytes in 0 blocks
==2300== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==2300==
==2300== All heap blocks were freed -- no leaks are possible
==2300==
==2300== For counts of detected and suppressed errors, rerun with: -v
==2300== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
因为我在数组中添加了一个额外的元素,这里没有内存问题吗?我认为 valgrind 会发现最后一个元素有问题,因为它在数组之外。