+ 2

Don't variables have a fixed size. If so then I will know the size of the program when writing it. So why do we require heap mem

I'm new to programming so I cant really understand the technical stuff.

20th Jun 2018, 8:12 AM
Devesh Mungad
3 Answers
21st Jun 2018, 4:46 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
All runned programs have need of memory. This memory contain many data like instructions at low level format and memory for store so called "variable". Futhermore any OS has need other memory for own purpose. The memory occupied by program depends on many factors OS, compiler, compilation settings and (obviously) the data that your program allocate. Mainly program use two memory allocation type: Stack and Heap. Stack its used when a you define local variable while Heap its used when your program allocate dynamically variables via new/malloc. There are differences: Stack its limited in size (by OS and compiler) but very fast because for access to data its needed only to decrement an adress (the entire stack its based on a last-in-first-out stack ) while the Heap has virtually no limits but not having a linear access its fragmented then OS have to implement an more "complex" algorithm for access to heap allocations.
20th Jun 2018, 9:02 AM
KrOW
KrOW - avatar
+ 2
Anyway you progran have need of dynamic allocations (heap memory) because it cannot always know how much data/memory its necessary for accomplished to an specific solution
20th Jun 2018, 9:03 AM
KrOW
KrOW - avatar