Я изучаю 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;
}
Я получаю ошибки компилятора:
[email protected]:~/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 найдет проблему с последним элементом, потому что она находится вне массива.
И как это можно обнаружить во время выполнения? –