3

The size of a static member variable is not counted by the sizeof operator, so I guess it's in the data segment. Am I right?

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
coinsyx
  • 643
  • 2
  • 7
  • 13
  • 5
    "Where does C++ place static member variable, in stack or in data segment." - it depends on the implementation. The C++ standard is not concerned with things like "executable segments" and stuff. There's no correct general answer apart from "it can be anything". –  Sep 15 '13 at 06:15
  • 3
    @H2CO3 Your general sentiment is true, but in this case it the C++ language semantics cannot be supported by placing static member variables on the stack. – Kevin A. Naudé Sep 15 '13 at 06:18
  • 2
    @KevinA.Naudé Then read it as "it can be anything apart from the stack, if any". There's no notion of "stack" in the Standard either. –  Sep 15 '13 at 06:20
  • 1
    There is no stack nor data segment mandated by the C++ standard. That is an implementation issue. It's not even required for an implementation to _have_ a stack provided it can do everything the C++ virtual machine is required to without one. – paxdiablo Sep 15 '13 at 06:35
  • 1
    "The size of static member variable is not counted by sizeof operator." Yes it is. The rest of your question is a *non sequitur.* – user207421 Sep 15 '13 at 07:24

2 Answers2

3

Correct guess. Static members are essentially global variables, but not in the global variable namespace. Their storage is in the data segment.

EDIT: If an implementation finds it possible to squirrel away the static members in some unusual storage location, it would not matter. The semantics of static members remains the same, and the natural place for their storage is with global data. Static members are not part of object instances, so they don't count towards the size.

I think this is a sufficiently precise response to the question, as there is no indication that coinsyx is requesting formal notes from the C++ standard.

I will add that Trojanfoe is correct for legacy x86 systems, and possible others as well. In such cases, initialised variables would typically go in the data segment, while unitialised data would go in the bss segment. Furthermore, the primary stack may also be allocated from the bss segment. Other thread stacks would be allocated elsewhere. The static members would not be considered to be stack allocated.

Kevin A. Naudé
  • 3,992
  • 19
  • 20
  • 1
    Kevin, there's nothing stopping an implementation from allocating a lot of space in the `main()` stack frame for all variables of static storage duration. That's on the stack :-) Basically, the standard doesn't mandate this level of detail, implementations can do as they please provided the follow the rules of the virtual machine as laid out in the standard. – paxdiablo Sep 15 '13 at 06:38
  • They will only go in the data segment if they are initialized; uninitialized global variables will go into the bss segment. – trojanfoe Sep 15 '13 at 07:43
2

Static data, including static members, are placed in the data segment. But why do you think the sizeof operator doesn't count the size of static member variables? sizeof gives the size of the type of the variable.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
TheDuke
  • 750
  • 1
  • 7
  • 22
  • http://stackoverflow.com/questions/4640989/how-do-static-member-variables-affect-object-size – rano Sep 15 '13 at 06:17