0

I think of stack and heap as referring to different sections of a process' virtual memory, where one grows upwards and the other downwards. Physical memory addresses (in the form of pages) are mapped to these virtual addresses. AFAIK, the addressing of the virtual address space is from 0x00000000 to 0xFFFFFFFF, which is humongous. Since the area between the stack and heap is so huge (most likely much larger than the actual physical memory on the computer), I don’t think the stack and heap can ever meet each other. Also, what then limits the amount of heap memory a program can ask from the OS?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    There is only 1 [Virtual Address Space](https://learn.microsoft.com/en-us/windows/win32/memory/virtual-address-space) for the entire process, but portions of it are reserved for system use. Each thread has its own stack allocated, that is owned by the system, separate from heap memory. It is the OS's job to keep them separate, just as the compiler keeps your variables separate. Virtual memory is backed by not just installed RAM, but also HDD space (ie, swap file, etc). – Remy Lebeau Apr 24 '20 at 23:10
  • 1
    Here are some good diagrams: https://i.stack.imgur.com/jZRm8.gif https://i.stack.imgur.com/6Z9wZ.png – Remy Lebeau Apr 24 '20 at 23:16
  • What do you think stack overflow means? How about an infinitely recursive function? – babon Apr 25 '20 at 07:21

0 Answers0