0

Are stack memory and heap memory allocated in different locations in memory? In class that’s how they taught us it works. Specifically in c++, when allocating memory on the heap, you need to keep track of it with pointers, cause if you lose where it is, it could be anywhere in the computer so the compiler doesn’t know where to deallocate it. At least that’s how I interpreted it.

But a student in my class, who’s a really good programmer and knows a lot about programming, said that heap and stack can be stored next to each other. And what you’re doing when you allocate memory on the heap, is your telling the compiler to keep this memory indefinitely. Vs normal stack memory tells the compiler to only keep this memory allocated for as long as the program is running?

So which is it? A mixture of both? Neither? A lot more complicated than a simple analogy? Thank you.

I was expecting it to the two different types of memory to be in different locations in the computer. But now I’m not sure.

ZacharyyK
  • 9
  • 1
  • 2
    From your code's perspective, the exact location in memory doesn't really matter. The operating system handles all that. Your colleague is basically correct, if you understand "indefinitely" to mean "until I write other code that deletes the memory I just allocated". In C++ standardese, the concepts you're discussing are called "automatic storage duration" and "dynamic storage duration", and the use of a stack or a heap are an implementation detail. The key issue is when the lifetime of the object ends. – Nathan Pierson Apr 25 '23 at 22:27
  • https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Pawel Apr 25 '23 at 22:29
  • 3
    No sane operating system will allow your program to hold memory indefinitely. It will all be cleaned up when it ends (unless you are dealing with bare metal system or some other rare stuff). Objects allocated on the heap will live until relevant `delete` is called. Objects allocated on the stack will live until the end of current scope (usually, until nearest `}`). – Yksisarvinen Apr 25 '23 at 22:30
  • https://stackoverflow.com/questions/58357075/how-virtual-memory-looks-like-when-a-process-is-created-windows – Mooing Duck Apr 25 '23 at 22:30
  • 2
    In the old days, memory management was much more rigid and this view is still being taught, partly as [Lies-to-children](https://en.wikipedia.org/wiki/Lie-to-children) and partly because it costs money and time to update a curriculum. These days [virtual memory](https://en.wikipedia.org/wiki/Virtual_memory) has the stuff scattered all over the place. – user4581301 Apr 25 '23 at 22:40
  • 1
    And to add more chaos, C++ need not be implemented with stacks and heaps. – user4581301 Apr 25 '23 at 22:42
  • you can make a program that creates some variables and prints out their addresses – user253751 Apr 25 '23 at 23:00
  • A classic memory model is to have the stack at the bottom of memory and grows "upward" and the heap starts at the top of memory and grows "downward". And yes, having the stack and heap intersect was a problem. – Thomas Matthews Apr 25 '23 at 23:04
  • Let's consider the world of Embedded Systems. The stack can be located anywhere, such as fast memory on the System-On-A-Chip (SOC). The heap would usually be allocated on slower, cheaper and more plentiful memory. There is also possibility that an EMMC is used for heap memory. – Thomas Matthews Apr 25 '23 at 23:10
  • @ThomasMatthews there is precisely zero chance that the stack and heap are not the same type of memory or that either one is EMMC – user253751 Apr 25 '23 at 23:21
  • @user253751: I have worked on embedded systems were the stack and heap were on different memory types. The stack was allocated on the faster memory and heap on the slower (cheaper) memory. So, why is there zero percent chance, especially in the embedded systems world? – Thomas Matthews Apr 25 '23 at 23:24
  • some have **a** stack that is different, but never the C++ stack – user253751 Apr 25 '23 at 23:25

1 Answers1

0

Your understanding of the different types of memory in the first paragraph are correct. I know that memory on the heap can be "forgotten" about after program termination; but most operating systems have processes in place that serve as a sort of clean up.

nore
  • 63
  • 1
  • 7