By Jwags

2014-05-06 20:59:47 8 Comments

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!


@Nenad 2014-05-06 21:21:30

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.

@Dale Wilson 2014-05-06 21:23:00

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.

@Nenad 2014-05-06 21:26:56

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.

@Dale Wilson 2014-05-06 21:29:46

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.

@Nenad 2014-05-06 21:32:49

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).

@Dale Wilson 2014-05-06 21:20:08

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.)

@David S. 2014-05-06 21:13:49

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.

@Mr. Llama 2014-05-06 21:06:44

"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.

@Hot Licks 2014-05-06 21:07:47

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.

@Mr. Llama 2014-05-06 21:14:35

@HotLicks - At which point we'd be going by a dictionary definition, not a computer science one.

@Hot Licks 2014-05-06 21:21:07

So you would claim that the ONLY exception to using the term "heap" to represent a graph is the C/C++/Java heap???

@Mr. Llama 2014-05-06 21:44:32

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 2014-05-06 21:50:22

@HotLicks - Correct, which is also why it's probably better to refer to it as the "memory pool" or something similar. :P

@Hot Licks 2014-05-06 21:53:44

Well, that's a heap of something, certainly. Always nice to arbitrarily redefine terms that have been in common use for 50 years.

@stix 2014-05-06 21:07:40

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.

@Hot Licks 2014-05-06 21:08:48

A "heap" may be a collection of similar objects that are not simply chunks of free storage.

@stix 2014-05-06 21:14:07

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."

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Which is faster: Stack allocation or Heap allocation

2 Answered Questions

[SOLVED] Swift stack/heap understanding

8 Answered Questions

[SOLVED] Where in memory are my variables stored in C?

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

25 Answered Questions

[SOLVED] What and where are the stack and heap?

1 Answered Questions

3 Answered Questions

[SOLVED] Class members and explicit stack/heap allocation

Sponsored Content