Why do we need to store data in The Heap ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why do we need to store data in The Heap ?

We know that in C++ there are to places to store data - 1. The Stack and 2. The Heap . So why do we store and use the data in heap while we can just use the stack ? I know it maybe explained in the course but I am having problem to understand this . Please Help .

8th Oct 2021, 8:45 AM
Pritam Santra
Pritam Santra - avatar
4 Answers
+ 1
What would you do when you don't know the maximum amount of memory your program would be allocating ?
8th Oct 2021, 8:57 AM
Arsenic
Arsenic - avatar
0
Arsenic Ya I know that sometimes the data need is not known . But the stack doesnt has any limits for data ?
8th Oct 2021, 9:05 AM
Pritam Santra
Pritam Santra - avatar
0
Scope is also one consideration, heap based memory lifetime is not limited as is the stack based memory. Memory allocated on the heap stays until explicitly destroyed, with the exception of smart pointers, *probably* ... https://stackoverflow.com/questions/40701228/why-do-we-use-heap-to-store-memory
8th Oct 2021, 9:18 AM
Ipang
0
Not only two memory area stack, heap are available read about assembly you will understood better about memory concepts and working process.
8th Oct 2021, 9:52 AM
A S Raghuvanshi
A S Raghuvanshi - avatar