What are the Windows and Linux native OS/system calls made from malloc()?

后端 未结 5 1445
生来不讨喜
生来不讨喜 2020-12-31 15:19

I recently saw the following post:

A memory allocator isn\'t lower level than malloc. (The default allocator typically calls malloc directly or indi

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 16:00

    In Windows, in recent versions of MSVC, malloc (and C++ new, as it is implemented using the same fundamentals for the actual memory allocation part of new) calls HeapAlloc(). In other versions, such as g++ mingw, the C runtime is an older version, which doesn't call quite as directly to HeapAlloc, but at the base of it, it still goes to HeapAlloc - to find something different, we need to go back to Windows pre-95, which did have a GlobalAlloc and LocalAlloc set of functions - but I don't think people use 16-bit compilers these days - at least not for Windows programming.

    In Linux, if you are using glibc, it depends on the size of the allocation whether it calls sbrk or mmap - mmap (with MAP_ANONYMOUS in the flags) is used for larger allocations (over a threshold, which I believe is 2MB in the typical implementation)

提交回复
热议问题