Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heap-Dynamic or Stack-Dynamic?

Please explain to me if "x" is a Stack-Dynamic variable or Heap-Dynamic variable in this code?And if it is Heap-Dynamic then why it is not Stack-Dynamic variable?Thank you

function foo(){ MyClass x = new MyClass();}
like image 975
Cheeta Avatar asked Dec 21 '22 11:12

Cheeta


1 Answers

Stack dynamic variables come into existence when you call a function. They exist on the C++ runtime stack, and are temporary. They are either in the parameter list, or declared inside the function (except for statics, which are not instantiated on the stack). These variables disappear when they go out of scope, and the memory for their contents is reclaimed by the runtime.

Heap dynamic instances exist in another area of memory the runtime sets aside called the "heap." These instances come into being via. the "new" operator, and must be explicitly deallocated by the "delete" operator.

I hope this is helpful

like image 142
python Avatar answered Jan 02 '23 22:01

python