Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the pointer holding dynamic variable in the stack or heap?

Tags:

c++

I want to get something clear that I found most sources so confusing.

For example,

int *a=new int;

Is "a" in stack or heap, and what about "*a"? Most sources I found only refer to heap, I really need an extremely concrete answer. I would be really grateful.

like image 632
Tjh Thon Avatar asked Mar 01 '26 08:03

Tjh Thon


1 Answers

a is in the stack. When the scope of a ends, a is not usable.

*a is in the heap. Even after the scope of a ends, the object that a points to continues to live unless the memory is deallocated before that.

like image 115
R Sahu Avatar answered Mar 03 '26 21:03

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!