Maiss Maiss - 3 months ago 10
C++ Question

What goes to RAM, Harddrive, Stack and Heap in C++?

Could someone tell in general what goes to what (Harddrive,RAM, Stack or Heap) at runtime in C++ for these instances :


  • Local/global variables

  • Classes, Methods and functions

  • Pointers

  • Objects



And is Stack/Heap both located in physical RAM?

I would appreciate if someone could include hardware analogy in the answer.
Thanks.

Answer

This is generally dependent on OS, but it's generally like so:

Everything goes to RAM. The binary resides in the hard-drive, but, when ran, is fully loaded, along with dependent libraries, into RAM.

Stack and heap are implementation details, but they also reside in the RAM.

Although loaded in RAM, the memory is not directly addressable. The operating system allocates virtual memory for each process. This means that the address 0x001 is not actually located at 0x001 in the RAM, but represents an address in virtual address space.

EDIT: Clarification to one of op's comments:

Are binaries fully or partially loaded at runtime? And, are those binaries only accessed once at runtime or continuisly being read from Harddrive?

For example, in MS, if you link against a library, it will be fully loaded at runtime, at the start of the program. If you load it programatically, via LoadLibrary(), it is loaded into memory at the function call, and can be unloaded from memory.