gpt4 book ai didi

c++ - 使用 std::ios::sync_with_stdio(false) 打印时在 Valgrind 的 LEAF SUMMARY 中获取 "still reachable"

转载 作者:行者123 更新时间:2023-11-30 02:13:33 25 4
gpt4 key购买 nike

考虑下面的简单代码:

#include <iostream>
#include <algorithm>
#include <vector>

void print_vector( const std::vector<int> &inputVector )
{
std::ios_base::sync_with_stdio(false);

for ( const int &p : inputVector )
{
std::cout << p << " ";
}

std::cout << "\n";
}

int main() {

std::vector<int> s{ 5, 7, 4, 2, 8, 6, 1, 9, 0, 3 };

std::sort( s.begin(), s.end(), [](const int &a, const int &b)
{
return a > b;
});

print_vector( s );

return 0;
}

当使用上述打印功能时,我在使用 valgrind 进行分析时在 LEAK SUMMARY 中得到了“仍然可以访问”。

我使用以下进行编译:(gcc 版本 9)

g++ --std=c++17 -Wall sorting_stl.cc -o sorting_stl.o

和下面的 valgrind 命令

valgrind --leak-check=full --show-leak-kinds=all -v ./sorting_stl.o 

最后的完整泄漏摘要:

LEAK SUMMARY:
definitely lost: 0 bytes in 0 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 0 bytes in 0 blocks
still reachable: 122,880 bytes in 6 blocks
suppressed: 0 bytes in 0 blocks

删除 std::ios::sync_with_stdio(false); 并使用 std::endl;解决错误。我不明白为什么会发生这种情况,或者这里是否有错误。通常使用 std::sync_with_stdio(false);

是个好主意吗

最佳答案

It's fine.

这算作“误报”;标准库的某些方面故意不进行清理。 This is one of them.

这不是随着时间的推移会变得更糟的泄漏。别担心。

理想情况下 Valgrind 应该是友好的并且会过滤掉它,但显然这是一个问题 for a long time .

您可以阅读更多关于 the various report levels in the Valgrind FAQ 的信息.

关于c++ - 使用 std::ios::sync_with_stdio(false) 打印时在 Valgrind 的 LEAF SUMMARY 中获取 "still reachable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59104808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com