Unsolved Memory Layout for Static Variable in Class
-
Hello All,
class hello { static int *x; }; int * hello::x = new int;
Does the variable x resides in data segment or Heap segment ?
I am really confused on this. Any help is deeply appreciated.
Thanks in advance
-
@Vinoth-Rajendran4
I assume the variable lives in the data segment, but what it points to afternew ...
resides on heap? -
@JonB , Thanks for the clarification.
I have a question,
Why static member variable not part of class memory ??
(I have read that static member variable are not part of object instance, but its common for class, so that they can be accessed across objects , but is there any other reason for justification ??) -
@Vinoth-Rajendran4
Depends what you mean by "class memory", but as you say a static variable is never part of a class instance, so it will be stored (once) outside of any memory area allocated on creating an instance. Yes, it can be accessed across objects/instances precisely because it's in some shared area.