0

I search for hours and get thousands of dicussions but get confused. I'm fresher and I want to simply ask my questions.

Where is the location of heap memory and stack memory in RAM?

Is it like that, when a certain part of the memory is used as a stack data structure, it is called stack memory? And if the same memory is used as a heap, it is called heap memory? I mean, Are they using same memory location in different times?

Or are there separate space for stack and heap in memory? I mean RAM is split into portions for performing stack and heap operations.

trincot
  • 317,000
  • 35
  • 244
  • 286
shafiq
  • 1
  • 2
  • 1
    ***Where is the location of heap memory and stack memory in RAM?*** As far as the c++ language is concerned a stack need not exist. This is an implementation detail. – drescherjm Dec 31 '20 at 22:01
  • You didn't need to search for hours, or read thousands of discussions... you only needed to read a book on computer architecture. – Asteroids With Wings Dec 31 '20 at 22:02
  • ***Are they using same memory location in different times?*** Modern operating systems will randomize the layout: [https://en.wikipedia.org/wiki/Address_space_layout_randomization](https://en.wikipedia.org/wiki/Address_space_layout_randomization) – drescherjm Dec 31 '20 at 22:06
  • 2
    On most operating systems when a program needs more memory it'll ask the os for a new page of memory, those pages could be stored anywhere in ram, there is no difference between stack and heap memory other than how the items within the memory are laid out – Alan Birtles Dec 31 '20 at 22:08
  • More of a valuable lesson than an opinion - it's not too late to go on Amazon and buy that book! – Asteroids With Wings Dec 31 '20 at 22:15
  • ***Or are there separate space for stack and heap in memory?*** Usually a stack is a MB or a few MB of continuous space while a heap can be scattered throughout the address space. – drescherjm Dec 31 '20 at 22:21
  • Totally depends. Memory management is extremely weird. You think you got RAM? Could be cache. Could be a hard disk. Could be anything. Could be nothing, because if you haven't actually used it there's no reason for the CPU to have wasted time actually tracking down memory to give you, just OKed that you can get it some time in the future If there's any available. To answer your question, you do need a book. – user4581301 Dec 31 '20 at 22:40
  • Please go easy on the **random** text written in **bold**. It doesn't help. – tadman Dec 31 '20 at 22:44

1 Answers1

3

Heap memory and stack memory, if that distinction is made by the compiler, are just two arbitrary chunks of memory. Heap is expected to be expandable, the stack is typically fixed-size, but this is not a hard requirement and cannot be depended on to be the case. These are just conventions, not rules.

Where are they located in memory? With virtual memory and Address Space Layout Randomization (ASLR) the answer is: Nobody knows and it doesn't matter, as the addresses are fake and have no relationship to physical memory. They're just numbers.

Is there a separate space for stack and heap? Maybe! Probably. Possibly not. On very tiny embedded platforms it's all one chunk of memory where the stack is stuck at one end and grows down, while the heap is at another and grows up. On a modern operating system they're allocated independently and can be extended or shrunk as required.

tadman
  • 208,517
  • 23
  • 234
  • 262