Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Memory Layout for Static Variable in Class
Forum Update on Monday, May 27th 2025

Memory Layout for Static Variable in Class

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 2 Posters 490 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • V Vinoth Rajendran4

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Vinoth-Rajendran4
      I assume the variable lives in the data segment, but what it points to after new ... resides on heap?

      1 Reply Last reply
      1
      • V Offline
        V Offline
        Vinoth Rajendran4
        wrote on last edited by
        #3

        @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 ??)

        JonBJ 1 Reply Last reply
        0
        • V Vinoth Rajendran4

          @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 ??)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved