std::array 的堆分配
Heap allocation for std::array
根据 std::array
分配在栈上。然而,当它与 Valgrind
一起使用时,它显示了堆分配,即使是在堆栈上分配的元素也是如此。这是误报还是真实的?
这里有两个 mwe
来说明行为。
无堆:
以下代码:
#include <array>
int main() {
std::array<int*, 1> map;
int value = 0;
}
产生预期的以下 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
有堆:
但是,如果我尝试此代码:
#include <array>
int main() {
std::array<int*, 1> map;
int value = 0;
map.at(0) = &value;
}
Valgrind
给我
==14539== HEAP SUMMARY:
==14539== in use at exit: 72,704 bytes in 1 blocks
==14539== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==14539==
==14539== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==14539== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14539== by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==14539== by 0x40106B9: call_init.part.0 (dl-init.c:72)
==14539== by 0x40107CA: call_init (dl-init.c:30)
==14539== by 0x40107CA: _dl_init (dl-init.c:120)
==14539== by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14539==
添加编译设置:
g++ -std=c++11 -O0 valgrind.cpp -o valgrind_build -I ../fake -I ../src
valgrind --track-origins=yes --dsymutil=yes --leak-check=full --show-leak-kinds=all ./valgrind_build
valgrind --version
valgrind-3.11.0
g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
代码
map.at(0) = &value;
引入边界检查,这可能反过来需要使用动态分配的东西(例如来自 <iostream>
库)。
您可以使用
再试一次
map[0] = &value;
不应用绑定检查。
根据std::array
分配在栈上。然而,当它与 Valgrind
一起使用时,它显示了堆分配,即使是在堆栈上分配的元素也是如此。这是误报还是真实的?
这里有两个 mwe
来说明行为。
无堆:
以下代码:
#include <array>
int main() {
std::array<int*, 1> map;
int value = 0;
}
产生预期的以下 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
有堆:
但是,如果我尝试此代码:
#include <array>
int main() {
std::array<int*, 1> map;
int value = 0;
map.at(0) = &value;
}
Valgrind
给我
==14539== HEAP SUMMARY:
==14539== in use at exit: 72,704 bytes in 1 blocks
==14539== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==14539==
==14539== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==14539== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14539== by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==14539== by 0x40106B9: call_init.part.0 (dl-init.c:72)
==14539== by 0x40107CA: call_init (dl-init.c:30)
==14539== by 0x40107CA: _dl_init (dl-init.c:120)
==14539== by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==14539==
添加编译设置:
g++ -std=c++11 -O0 valgrind.cpp -o valgrind_build -I ../fake -I ../src
valgrind --track-origins=yes --dsymutil=yes --leak-check=full --show-leak-kinds=all ./valgrind_build
valgrind --version
valgrind-3.11.0
g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
代码
map.at(0) = &value;
引入边界检查,这可能反过来需要使用动态分配的东西(例如来自 <iostream>
库)。
您可以使用
再试一次map[0] = &value;
不应用绑定检查。