0
Why C++ has 2 memories? And why should I use the 2 memories???
3 Answers
+ 1
Are you talking about stack vs. heap?
0
Yes please could you answer me ??
0
Both stack and heap are in RAM. However, accessing the stack is much faster than accessing the heap. This is why we use pointers in C++.
If I create a pointer to an object 'Enemy' as below:
Enemy* myEnemy = new Enemy();
Then we can do things much more quickly by passing around the address of myEnemy rather than a copy of the entire object.
The 'new' keyboard says to allocate new memory on the heap. Also, the stack is used to keep track of which subroutine the logic should return to after execution is completed (in a function for example).