Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a stack vs the stack and a heap vs the heap

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!

like image 859
Jwags Avatar asked May 06 '14 20:05

Jwags


1 Answers

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

like image 120
Mr. Llama Avatar answered Oct 05 '22 21:10

Mr. Llama