为什么valgrind没有检测到数组中的多余元素(why doesn't valgrind detect excess elements in arrays)

编程入门 行业动态 更新时间:2024-10-28 08:18:15
为什么valgrind没有检测到数组中的多余元素(why doesn't valgrind detect excess elements in arrays)

我正在学习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会发现最后一个元素有问题,因为它在数组之外。

I am learning C and going through a tutorial that includes the use of valgrind. I am still learning about what valgrind is actually doing and was wondering if someone could explain why it does not detect any errors in the following code:

#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; }

I do get compiler errors:

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]

But when I run it against valgrind it doesn't see anything wrong:

==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)

Isn't there a memory issue here because I am adding an extra element to the array? I thought valgrind would find a problem with the last element because it is outside the array.

最满意答案

您的array存储在stack area , Valgrind检查stack area中的泄漏。它会检查dynamic allocation的内存是否泄漏,因此您无法通过Valgrind任何检测。

如果你真的想看到效果,那么使用下面的代码

int main() { int *p=malloc(6); }

并使用Valgrind检查内存泄漏。

Your array is stored in stack area and Valgrind checks for the leak in heap area.It check for leak of memory allocated by dynamic allocation so you are not getting any detection by Valgrind.

If you really want to see the effect then use bellow code

int main() { int *p=malloc(6); }

and use Valgrind to check the memory leak.

更多推荐

本文发布于:2023-07-04 17:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027083.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测到   多余   组中   元素   valgrind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!