为什么瓦尔格林德没有检测到使用了未初始化的值

  • 本文关键字:初始化 林德没 valgrind
  • 更新时间 :
  • 英文 :

#include <stdlib.h>
int* matvec(int A[][3], int* x, int n) {
int i, j;
int* y = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
y[i] += A[i][j] * x[j];
}
}
free(y);
}
void main() {
int a[3][3] = {
{0, 1, 2},
{2, 3, 4},
{4, 5, 6}
};

int x[3] = {1, 2, 3};
matvec(a, x, 3);
}

我通过以下命令检测内存问题:valgrind --tool=memcheck --leak-check=full --track-origins=yes ./a.out它给出输出:

==37060== Memcheck, a memory error detector
==37060== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==37060== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==37060== Command: ./a.out
==37060== 
==37060== 
==37060== HEAP SUMMARY:
==37060==     in use at exit: 0 bytes in 0 blocks
==37060==   total heap usage: 1 allocs, 1 frees, 12 bytes allocated
==37060== 
==37060== All heap blocks were freed -- no leaks are possible
==37060== 
==37060== For lists of detected and suppressed errors, rerun with: -s
==37060== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

问题是:为什么valgind没有检测到:y数组被用作未初始化的的问题

来自Valgrind用户手册§4.2.2未初始化值的使用:

只有当您的程序试图以可能影响程序外部可见行为的方式使用未初始化的数据时,才会发出投诉。

您从一些未初始化的内存开始,保持它未初始化,然后释放它。您没有以外部可见的方式使用它,例如在if条件中使用它或将它传递给系统调用。

最新更新