Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3
-----Stack----- * Stored in computer RAM just like the heap. * Variables created on the stack will go out of scope and are automatically deallocated. * Much faster to allocate in comparison to variables on the heap. * Implemented with an actual stack data structure. * Stores local data, return addresses, used for parameter passing. * Can have a stack overflow when too much of the stack is used (mostly from infinite or too deep recursion, very large allocations). * Data created on the stack can be used without pointers. * You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big. * Usually has a maximum size already determined when your program starts. -----Heap----- * Stored in computer RAM just like the stack. * In C++, variables on the heap must be destroyed manually and never fall out of scope. The data is freed with delete, delete[], or free. * Slower to allocate in comparison to variables on the stack. * Used on demand to allocate a block of data for use by the program. * Can have fragmentation when there are a lot of allocations and deallocations. * In C++ or C, data created on the heap will be pointed to by pointers and allocated with new or malloc respectively. * Can have allocation failures if too big of a buffer is requested to be allocated. * You would use the heap if you don't know exactly how much data you will need at run time or if you need to allocate a lot of data. * Responsible for memory leaks. https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap
3rd Sep 2018, 3:18 PM
Steppenwolf
Steppenwolf - avatar