相关疑难解决方法(0)

Valgrind:使用<iostream>可以通过简单的程序访问内存

采取以下琐碎的计划:

#include <iostream>
int main() {
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我使用valgrind运行它,我被告知有72,704 bytes in 1 blocks那些still reachable.关于是否担心仍然可以达到警告的SO已经进行了广泛的讨论 - 我并不关心这一点.我只想了解当程序本身没有分配该库中的任何对象时,如何简单地包含标准库头可能会导致仍然可以访问的警告.

这是完整的valgrind输出:

$ valgrind --leak-check=full --track-origins=yes --show-reachable=yes ./ValgrindTest
==27671== Memcheck, a memory error detector
==27671== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==27671== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==27671== Command: ./ValgrindTest
==27671== 
==27671== 
==27671== HEAP SUMMARY:
==27671==     in use at exit: 72,704 bytes in 1 blocks
==27671== …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind

68
推荐指数
3
解决办法
2万
查看次数

std :: array的堆分配

根据这个问题 std::array分配在堆栈上.但是,当它与它一起使用时,Valgrind它显示了堆分配,即使对于在堆栈上分配的元素也是如此.这是假阳性还是真实?

这里按照两个mwe来说明行为.

没有堆:

以下代码:

#include <array>

int main() {
    std::array<int*, 1> map;
    int value = 0;
}
Run Code Online (Sandbox Code Playgroud)

产生预期的以下Valgrind输出:

==14425== HEAP SUMMARY:
==14425==     in use at exit: 0 bytes in 0 blocks
==14425==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
Run Code Online (Sandbox Code Playgroud)

用堆:

但是,如果我尝试这个代码:

#include <array>

int main() {
    std::array<int*, 1> map;
    int value = 0;

    map.at(0) = &value;
}
Run Code Online (Sandbox Code Playgroud)

Valgrind 给我

==14539== HEAP SUMMARY:
==14539==     in use at exit: 72,704 …
Run Code Online (Sandbox Code Playgroud)

c++ arrays valgrind c++11 stdarray

5
推荐指数
1
解决办法
443
查看次数

标签 统计

c++ ×2

valgrind ×2

arrays ×1

c++11 ×1

stdarray ×1