从man页面,
MAP_ANONYMOUS
The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require
fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. The use of MAP_ANONYMOUS in conjunction with
MAP_SHARED is only supported on Linux since kernel 2.4.
Run Code Online (Sandbox Code Playgroud)
使用目的是MAP_ANONYMOUS什么?任何一个例子都会很好.还要从哪里映射内存?
在man页面上写的The use of MAP_ANONYMOUS in conjunction with MAP_SHARED is only supported on Linux …
代码很简单:
#include <vector>
int main() {
std::vector<int> v;
}
Run Code Online (Sandbox Code Playgroud)
然后,我在Linux上使用Valgrind构建并运行它:
g++ test.cc && valgrind ./a.out
==8511== Memcheck, a memory error detector
...
==8511== HEAP SUMMARY:
==8511== in use at exit: 72,704 bytes in 1 blocks
==8511== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==8511==
==8511== LEAK SUMMARY:
==8511== definitely lost: 0 bytes in 0 blocks
==8511== indirectly lost: 0 bytes in 0 blocks
==8511== possibly lost: 0 bytes in 0 blocks
==8511== still reachable: 72,704 bytes in …Run Code Online (Sandbox Code Playgroud)