int main() {
int *g = new int;
}
In the example above, *g is allocated dynamically on the heap and is module-global. Is that correct? And what about g? Is it also allocated dynamically on the heap and module-global?
Thank you for answering.
int main() {
int *g = new int;
}
In the example above, *g is allocated dynamically on the heap and is module-global. Is that correct? And what about g? Is it also allocated dynamically on the heap and module-global?
Thank you for answering.
First of all, the only variable you have is g
, which has type int*
. There is no g*
or *g
.
g is allocated dynamically on the heap ... Is that correct?
Not quiet. Memory allocation will happen on the right side of the =
. g
will be pointing to this newly allocated memory. You can access the allocated memory using g
. When the scope ends, g
will be destroyed without freeing the memory.
module-global?
No. And we don't have module globals in C++. We have static
, automatic
, dynamic
and thread
. g
variable in this case is automatic
, which basically means it will be destroyed as soon as the scope ends.