+ 2

Heap memory or stack

Hi int* pi = new int; Above line in c++ would create int type pointer variable on stack... is this true ? Why it is true or false ?

31st Jan 2020, 2:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Odpowiedź
+ 4
This is something similar in Java. Object obj = new Object(); Here obj is a a reference variable which is created on the stack and new Object() is created on the heap. In the above case obj holds the reference to the new Object(). You can say that obj is pointing to new Object() which is on the heap but obj itself is on the stack.
31st Jan 2020, 3:10 PM
Avinesh
Avinesh - avatar