Reputation: 3
In the function
int main() {
int *p=new int;
return 0;
}
is p
an explicit heap dynamic variable or stack dynamic variable?
I am aware that p
is a variable pointing to a value in the heap, but I am confused as to if p
is an explicit heap dynamic variable or stack dynamic variable as well.
Upvotes: 0
Views: 54
Reputation: 223795
A pointer is a separate thing from the thing it points to.
int *p
in block scope defines an object named p
with automatic storage duration. Objects with automatic storage duration are most commonly implemented with a stack except as altered by optimization, which may keep the object’s value in a register or, in some situations, eliminate it entirely.
The fact p
is initialized to point to dynamically allocated memory is irrelevant to the storage duration of p
. A pointer with any type of storage duration may point to an object with any type of storage duration.
Upvotes: 0