Sometimes I meet in translations into Russian of foreign literature, that the stack and the heap are sometimes used as synonyms. This is what I do not understand. After all, a heap is called a dynamic memory in which you can place different data types (new, malloc). And the stack is completely different. The stack is like a pile, that they put it up, then we take it. Or when they identify the stack and the heap, do they mean the heap as a data structure? Explain, but it is somehow dull and incomprehensible.

    2 answers 2

    About the stack is already clear. And about the "bunch" as a data structure - well described in the wiki . With the difference in terms of heaps - as a structure, and heaps - as a method of dynamic memory allocation.

    • So the question turns out, is it possible to use the heap as a data structure to make a stack? Who implements the stack, the operating system or is it a hardware feature? - abg
    • If we talk about dynamic memory allocation, the stack is a contiguous area of ​​memory referenced by the corresponding registers (for the x86 architecture, this is the ss: sp pair, well, and the "extended" versions for large digits). The processor, performing the command "push", "pop", changes these registers. Similarly, the stack is either growing or decreasing. Using assembler inserts, you can move the stack yourself - but it is EXTREMELY important that you return the stack to its initial state by the time you exit the procedure. - Majestio
    • If we talk about dynamic memory allocation, then a heap is a memory area where it is possible to place data. But there are no hardware heap management tools. The maximum that controls the hardware and operating system - is beyond the limits of valid addresses. Therefore, where and how to implement logical structures - all at the mercy of the developer. - Majestio

    Heap is not only dynamic memory. This (heap) is also a pyramid - a structure on which a pyramidal sort is drawn up, for example.

    True, the use of the pyramid as a stack is unlikely to succeed. Is it like a priority queue.