mmap在堆中分配内存?

Set*_*thu 9 mmap

我正在阅读维基百科中的mmap并尝试这个例子http://en.wikipedia.org/wiki/Mmap#Example_of_usage.我用gcc编译了这个程序并运行了valgrind overit.

这是valgrind输出:

# valgrind a.out 
==7018== Memcheck, a memory error detector
==7018== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==7018== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==7018== Command: a.out
==7018== 
PID 7018:   anonymous string 1, zero-backed string 1
PID 7019:   anonymous string 1, zero-backed string 1
PID 7018:   anonymous string 2, zero-backed string 2
==7018== 
==7018== HEAP SUMMARY:
==7018==     in use at exit: 0 bytes in 0 blocks
==7018==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==7018== 
==7018== All heap blocks were freed -- no leaks are possible
==7018== 
==7018== For counts of detected and suppressed errors, rerun with: -v
==7018== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
PID 7019:   anonymous string 2, zero-backed string 2
==7019== 
==7019== HEAP SUMMARY:
==7019==     in use at exit: 0 bytes in 0 blocks
==7019==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==7019== 
==7019== All heap blocks were freed -- no leaks are possible
==7019== 
==7019== For counts of detected and suppressed errors, rerun with: -v
==7019== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Run Code Online (Sandbox Code Playgroud)

我的问题是

mmap是否在Heap上分配内存?如果不是munmap做什么?

Oli*_*rth 19

在类Unix系统上,程序的地址空间由一个或多个虚拟内存区域组成,每个虚拟内存区域由操作系统映射到物理内存,文件或任何内容.

一般来说,堆是由C运行时创建的一个特定内存区域,并由malloc(通过使用brksbrk系统调用增长和收缩)进行管理.

mmap是一种创建新内存区域的方法,独立malloc于堆(并且独立于堆). munmap只是它的反面,它释放了这些区域.

  • 然而,有时 `malloc` 本身可能使用 `mmap` 而不是 `brk`/`sbrk`。 (3认同)

pio*_*kuc 5

mmapped内存既不是堆也不是堆栈.它被映射到调用进程的虚拟地址空间,但它没有在堆上分配.