0
What is Dynamic memory? And what is the heap?
I'm having difficulty with this lesson, I cant seem to understand what it is/what it is used for.
2 ответов
+ 1
Dynamic memory basically means allocation(reserving) and deallocating(freeing) memory at runtime. For example you may want to create an array with size that user inputs. In that case you will have to use dynamic memory allocating as you have no way of knowing the size of array to allocate at compile time.
You can think (for simplicity) that memory has basically two parts. Stack and heap.
Stack is closer to cpu and can be accessed faster by cpu than heap
Stack is relatively small in size
Stack is also local to the scope(which means if you allocate in stack you can't use that object out of that scope)
Heap is relatively large(usually much larger than stack) and is relatively slower than stack and isn't limited to a scope.
You should allocate on heap if the object you're allocating is large or if you want to use it in other scopes. Most of the time you should allocate in stack.