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 Updated to NodeBB v4.3 + New Features

Memory Layout for Static Variable in Class

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 2 Posters 501 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 29 Oct 2019, 13:56 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

    J 1 Reply Last reply 29 Oct 2019, 13:58
    0
    • V Vinoth Rajendran4
      29 Oct 2019, 13:56

      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

      J Offline
      J Offline
      JonB
      wrote on 29 Oct 2019, 13:58 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 29 Oct 2019, 14:34 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 ??)

        J 1 Reply Last reply 29 Oct 2019, 15:58
        0
        • V Vinoth Rajendran4
          29 Oct 2019, 14:34

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

          J Offline
          J Offline
          JonB
          wrote on 29 Oct 2019, 15:58 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

          2/4

          29 Oct 2019, 13:58

          • Login

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