Sign in to follow this  
formalproof

How does C++ memory management work under the hood?

Recommended Posts

formalproof    165
How does ANSI C++ memory management actually work under the hood in Windows? When I use, for example, the "new" keyword in my C++ program, does the compiler translate it directly to some Win32 API call? If yes, then which one? Or will the compiled code call some function in the Visual C++ runtime libraries? Or something different? Thanks for the answers :)

Share this post


Link to post
Share on other sites
CTar    1134
It's really up to the compiler, and it can do whatever it wants, but in my experience many simply create a Win32 memory heap and call HeapAlloc.

See here for an overview of the heap memory management functions. For a more in-depth view of how Windows handles a call to HeapAlloc see this (note it's fairly long as it's an actual chapter from a book).

Share this post


Link to post
Share on other sites
Benden    1480
Also if you want to see what a function call does, place a breakpoint where it is called (in this case a call to new) and step into it (F11 in Visual Studio).

Share this post


Link to post
Share on other sites
Toolmaker    967
Quote:
Original post by Benden
Also if you want to see what a function call does, place a breakpoint where it is called (in this case a call to new) and step into it (F11 in Visual Studio).


Which only works if the sourcecode of that function is available. In the case of new, I doubt the source is provided and you end up in the disassembly of the function.

However, there's a chance it works.

Toolmaker

Share this post


Link to post
Share on other sites
Benden    1480
Quote:
Original post by Toolmaker
Quote:
Original post by Benden
Also if you want to see what a function call does, place a breakpoint where it is called (in this case a call to new) and step into it (F11 in Visual Studio).


Which only works if the sourcecode of that function is available. In the case of new, I doubt the source is provided and you end up in the disassembly of the function.

However, there's a chance it works.

Toolmaker


Works for me in VS2008 without any special setup. Jumps into new (new.cpp) to malloc in dbgmalloc.c without problems.

Share this post


Link to post
Share on other sites
thedustbustr    191
when you call 'new' on windows, the C++ runtime library may or may not do some bookkeeping and other tasks (like padding memory with debug values in a debug build). eventually the c++ runtime library will call HeapAlloc to get a pointer to a buffer of the size you requested. HeapAlloc in turn will, when needed, call VirtualAlloc. VirtualAlloc will in turn call some operating system kernel mode stuff to give you a pointer to a new page of memory. You can call HeapAlloc or VirtualAlloc yourself - there are some applications where you might legitimately do this - e.g. writing your own implementation of new, or allocating memory in a different process to do hackery with VirtualAllocEx.

Heap functions
Virtual memory functions

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this