eusrh eusrh - 27 days ago 11
C Question

What different attributes between malloc() and VirtualAlloc() to allocate a memory in windows?

The VirtualAlloc() will allocate a virtual memory page that have some attributes (by the parameter "fdwProtect").

What about the memory that allocated by malloc() ?Is that have same attributes?

Is the memory by malloc() have attributes that "commintting" or "reserving" ?

Further more, what about other C/C++ lib function?

Answer

VirtualAlloc

This function allows you to specify additional options for memory allocation. But it allocates memory in large page with a minimum indicated by GetLargePageMinimum, you can commit, reserve with it. It's not for general use. Memory allocated by this function is automatically initialized to zero.

malloc

The standard C version to allocate memory. Prefer it if you are writing in C rather than C++, and your code needs to work on on other platforms, or someone specifically says that you need to use it. It's quite possible that, on Windows, malloc would be implemented on top of HeapAlloc. malloc can allocate any chunk of memory, it doesn't have any concept to commit and reserve by current standard. Memory allocated by this function is not initialized.