8

I'm studying for my data organization final and I'm going over stacks and heaps because I know they will be on the final and I'm going to need to know the differences. I know what the Stack is and what the Heap is.

But I'm confused on what a stack is and what a heap is.

The Stack is a place in the RAM where memory is stored, if it runs out of space, a stackoverflow occurs. Objects are stored here by default, it reallocates memory when objects go out of scope, and it is faster.

The Heap is a place in the RAM where memory is stored, if it runs out of space, the OS will assign it more. For an object to be stored on the Heap it needs to be told by using the, new, operator, and will only be deallocated if told. fragmentation problems can occur, it is slower then the Stack, and it handles large amounts of memory better.

But what is a stack, and what is a heap? is it the way memory is stored? for example a static array or static vector is a stack type and a dynamic array, linked list a heap type?

Thank you all!

Jwags
  • 404
  • 4
  • 13
  • 3
    `I know what the Stack is and what the Heap is`, `but I'm confused on what a stack is and what a heap is.` Huh? – Rick S May 06 '14 at 21:04
  • 1
    "Stack" and "heap" are generic concepts. In C-based languages, "the stack" and "the heap" are specific entities -- "the stack" is the "execution stack" that manages call/return, auto variable storage, etc, and "the heap" is where you `malloc` or `new` pieces of storage. There can be other (user-defined) stacks and heaps that manage entirely different tasks. – Hot Licks May 06 '14 at 21:05
  • See http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29 and http://en.wikipedia.org/wiki/Heap_%28data_structure%29 – Hot Licks May 06 '14 at 21:06
  • 1
    Don't confuse the [Abstract Data Types (ADTs)](http://en.wikipedia.org/wiki/Abstract_data_type#Defining_an_abstract_data_type_.28ADT.29) with specific implementation concepts of a particular language (that are not strictly related to an ADT). – user2864740 May 06 '14 at 21:11
  • ..and, of course, none of this information is available on Google. – Martin James May 07 '14 at 00:36
  • This question appears to be off-topic because it is about regurgitating freely-available information. – Martin James May 07 '14 at 00:37

5 Answers5

8

"The stack" and "the heap" are memory lumps used in a specific way by a program or operating system. For example, the call stack can hold data pertaining to function calls and the heap is a region of memory specifically used for dynamically allocating space.

Contrast these with stack and heap data structures.

A stack can be thought of as an array where the last element in will be the first element out. Operations on this are called push and pop.

A heap is a data structure that represents a special type of graph where each node's value is greater than that of the node's children.

On a side note, keep in mind that "the stack" or "the heap" or any of the stack/heap data structures are unique to any given programming language but are simply concepts in the field of computer science.

Mr. Llama
  • 18,561
  • 2
  • 47
  • 99
  • That is a specific use of the general term "heap". One can have a "heap" of file handles, eg, that is not managed as a graph. – Hot Licks May 06 '14 at 21:07
  • 3
    @HotLicks - At which point we'd be going by a dictionary definition, not a computer science one. – Mr. Llama May 06 '14 at 21:14
  • So you would claim that the ONLY exception to using the term "heap" to represent a graph is the C/C++/Java heap??? – Hot Licks May 06 '14 at 21:21
  • Maybe I should be more specific: in the context of computer science, a "heap" is a tree type data structure that satisfies the heap property (where each node's value is less than its parent value). Calling anything else in computer science a "heap" would be misleading. For example, a collection of file handles *is not* a heap because it's 1) not a tree structure and 2) does not satisfy the heap property. – Mr. Llama May 06 '14 at 21:44
  • @HotLicks - Correct, which is also why it's probably better to refer to it as the "memory pool" or something similar. :P – Mr. Llama May 06 '14 at 21:50
  • Well, that's a heap of something, certainly. Always nice to arbitrarily redefine terms that have been in common use for 50 years. – Hot Licks May 06 '14 at 21:53
6

I won't get into virtual memory (read about that if you want) so let's simplify and say you have RAM of some size.

You have your code with static initialized data, with some static uninitialized data (static in C++ means like global vars). You have your code.

When you compile something compiler (and linker) will organize and translate your code to machine code (byte code, ones and zeroes) in a following way:

Binary file (and object files) is organized into segments (portions of RAM).

First you have DATA segment. This is the segment that contains values of initialized variables. so if u have variables i.e. int a=3, b = 4 they will go to DATA segment (4 bytes of RAM containing 00000003h, and other 4 bytes containing 000000004h, hexadecimal notation). They are stored consecutively.

Then you have Code segment. All your code is translated into machine code (1s and 0s) and stored in this segment consecutively.

Then you have BSS segment. There goes uninitialized global vars (all static vars that weren't initialized).

Then you have STACK segment. This is reserved for stack. Stack size is determined by operating system by default. You can change this value but i won't get into this now. All local variables go here. When you call some function first func args are pushed to stack, then return address (where to come back when u exit function), then some computer registers are pushed here, and finally all local variables declared in the function get their reserved space on stack.

And you have HEAP segment. This is part of the RAM (size is also determined by OS) where the objects and data are stored using operator new.

Then all of the segments are piled one after the other DATA, CODE, BSS, STACK, HEAP. There are some other segments, but they are not of interest here, and that is loaded in RAM by the operating system. Binary file also has some headers containing information from which location (address in memory) your code begins.

So in short, they are all parts of RAM, since everything that is being executed is loaded into RAM (can't be in ROM (read only), nor HDD since HDD its just for storing files.

Nenad
  • 305
  • 1
  • 6
  • 1
    What? you mean I'm not allowed to execute code in ROM? Oh Dear. I'm going to have to throw away a lot of work and start over. – Dale Wilson May 06 '14 at 21:23
  • No, you execute code from ROM but ROM is used to execute code when you start your computer. You push POWER button, comp turns on, starts reading from fixed address (that address is some ROM address). There is a small code that does the follwing: copy some code from ROM to RAM, continue execution of the copied code from RAM, do something, look into boot sector of HDD and copy OS boot file to RAM, execute it and start OS. After that you don't need to execute ROM, you double click some file to run, loader loads it into ROM and executes it. – Nenad May 06 '14 at 21:26
  • 1
    My point is that your answer is not accurate (it says "everything that is being executed is loaded into RAM) Not true. Your answer is also very specific to one machine architecture (admittedly a common one) and provides a whole lot of detail that doesn't really address the question you were responding to. – Dale Wilson May 06 '14 at 21:29
  • 1
    Yeah I meant cant be loaded into ROM. :) Details are there so the guy can understand the bigger picture. Other guys told him just what stack and heap are, and I explained the background (simplified). – Nenad May 06 '14 at 21:32
4

When specifically referring to C++'s memory model, the heap and stack refer to areas of memory. It is easy to confuse this with the stack data structure and heap data structure. They are, however, separate concepts.

When discussing programming languages, stack memory is called 'the stack' because it behaves like a stack data structure. The heap is a bit of a misnomer, as it does not necessarily (or likely) use a heap data structure. See Why are two different concepts both called "heap"? for a discussion of why C++'s heap and the data structure's names are the same, despite being two different concepts.

So to answer your question, it depends on the context. In the context of programming languages and memory management, the heap and stack refer to areas of memory with specific properties. Otherwise, they refer to specific data structures.

Community
  • 1
  • 1
David S.
  • 498
  • 3
  • 5
0

The technical definition of "a stack" is a Last In, First Out (LIFO) data structure where data is pushed onto and pulled off of the top. Just like with a stack of plates in the real world, you wouldn't pull one out from the middle or bottom, you [usually] wouldn't pull data out of the middle of or the bottom of a data structure stack. When someone talks about the stack in terms of programming, it can often (but not always) mean the hardware stack, which is controlled by the stack pointer register in the CPU.

As far as "a heap" goes, that generally becomes much more nebulous in terms of a definition everyone can agree on. The best definition is likely "a large amount of free memory from which space is allocated for dynamic memory management." In other words, when you need new memory, be it for an array, or an object created with the new operator, it comes from a heap that the OS has reserved for your program. This is "the heap" from the POV of your program, but just "a heap" from the POV of the OS.

stix
  • 1,128
  • 10
  • 30
  • A "heap" may be a collection of similar objects that are not simply chunks of free storage. – Hot Licks May 06 '14 at 21:08
  • That's true, but that's not the context in which the OP is working. It may make more sense to refer to the OP's "heap" as a "memory heap." – stix May 06 '14 at 21:14
0

The important thing for you to know about stacks is the relationship between the stack and function/method calls. Every function call reserves space on the stack, called a stack frame. This space contains your auto variables (the ones declared inside the function body). When you exit from the function, the stack frame and all the auto variables it contains disappear.

This mechanism is very cheap in terms of CPU resources used, but the lifetime of these stack-allocated variables is obviously limited by the scope of the function.

Memory allocations (objects) on the heap, on the other hand, can live "forever" or as long as you need them without regards to the flow of control of your program. The down side is since you don't get automatic lifetime management of these heap allocated objects, you have to either 1) manage the lifetime yourself, or 2) use special mechanisms like smart pointers to manage the lifetime of these objects. If you get it wrong your program has memory leaks, or access data that may change unexpectedly.

Re: Your question about A stack vs THE stack: When you are using multiple threads, each thread has a separate stack so that each thread can flow into and out of functions/methods independently. Most single threaded programs have only one stack: "the stack" in common terminology.

Likewise for heaps. If you have a special need, it is possible to allocate multiple heaps and choose at allocation time which heap should be used. This is much less common (and a much more complicated topic than I have mentioned here.)

Dale Wilson
  • 8,374
  • 2
  • 27
  • 47