when a local variable is initialized it is created on the stack. Can we create a local variable on heap memory? If so, what are the different ways in which we can do the same?
I am using c++
when a local variable is initialized it is created on the stack. Can we create a local variable on heap memory? If so, what are the different ways in which we can do the same?
I am using c++
Yes, when you initialize a local variable it gets memory from stack.
But if you want to initialize some local variable and use heap memory then use concept of Dynamic Memory Allocation
like this:
int *foo;
foo = new int [5];
This will initialize a int pointer
in stack but it will point to some memory location in heap
which you can use to store your foo
array.
Check this out for more understanding.