0

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++

f_puras
  • 2,521
  • 4
  • 33
  • 38
the_unknown_spirit
  • 2,518
  • 7
  • 34
  • 56

1 Answers1

0

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.

Milan Patel
  • 422
  • 4
  • 12