1

I would like to know whether the stack and heap referred to in stack and heap memory are actually implemented as stack and heap data structures?

I think the stack is actually a stack that has pointers to the LIFO (Last In First Out) variables declared in functions however I wanted to confirm and also ask whether the heap shares more than just its name to the dynamic tree data structure that satisfies the heap property? I have read a lot recently on the stack and heap and believe I understand the concept however it then made me curious about the actual implementation. I imagine it could be different on different architectures too and there may not be a specific general answer for all computers and OS's.

In case anyone comes upon this question who is still unsure of what and where are the stack and heap please see this question and other links I found useful in learning the stack and heap concept.

What and where are the stack and heap? http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/ https://www.youtube.com/watch?v=_8-ht2AKyH4

Community
  • 1
  • 1
joshuatvernon
  • 1,530
  • 2
  • 23
  • 45
  • Does this answer your question? [a stack vs the stack and a heap vs the heap](https://stackoverflow.com/questions/23504409/a-stack-vs-the-stack-and-a-heap-vs-the-heap) – trincot Aug 10 '21 at 14:32

2 Answers2

3

The memory heap is decidedly not a heap data structure. That is, it's not a priority queue. I suppose you could use a priority queue heap to build a memory heap, but there are much better ways to do it.

The stack is ... well ... Typically, the stack is a fixed block of memory allocated to the process. The processor itself treats that block of memory like a pure stack. That is, the processor's stack pointer points to the top of stack, and the push and pop instructions work as expected, adding things to and removing things from the stack. In that way, it's LIFO.

However, the processor can do all manner of things with the stack: push and pop different sized things, address directly into it (i.e. view the third item without popping the first two), etc. So although the processor stack does have push and pop instructions, it also has much more extended functionality. I wouldn't call it a pure LIFO data structure.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
1

Heap memory : It doesn`t have to be a heap data structure. A heap serves as a non-fixed size memory area on virtual memory and the functionalities it exposes to programmers are allocations and frees. A heap can be implemented with various data structures . Regarding native C and C++ development , the heap memory structure will be defined by the allocators you are using. They will be responsible for managing the necessary data structures and the virtual memory. In the link below, you can see the default implementations on Windows, Linux and MacOS : http://core-analyzer.sourceforge.net/index_files/Page335.html

Stack memory: It doesn`t have to be implemented as a stack ( first in last out ) data structure , however the functionality it exposes ( not to programmers) is in form of a stack data structure. ( Processor will treat that memory given by operating system as a stack data structure ) It is a fixed size memory area on virtual memory. In modern operating systems, stacks will be per thread , and you can also define that limit by using ulimit in Linux , on the other hand in Windows side it is a linker parameter : https://msdn.microsoft.com/en-US/library/8cxs58a6(v=vs.120).aspx

Here is also another nice reading : http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/

Akin Ocal
  • 471
  • 4
  • 11