0

In the following code snippet. The static member variable map is initializes using its default constructor.

#include <iostream>
#include <map>
using namespace std;

class A
{
 static map<int, int> m_map; //static member variable

 public:
 void PrintSize()
 {
     //accessing it
     //so that the map gets into the executable
     cout < m_map.size() << endl;
 }
};

// Initializing the static map member variable
map<int, int> A::m_map = map<int, int>();

int main()
{
     A a;
     cout << sizeof(a) << endl;
     a.PrintSize();

     return 0;
}

The program runs fine. My question is that where is the temporary variable formed to initialize the static map stored?

Scorpious
  • 133
  • 1
  • 9
  • 1
    What temporary variable? There is no temporary variable. – Šimon Tóth Aug 28 '12 at 09:49
  • 1
    @Let_Me_Be Formally, `A::m_map` is initialized by copy constructing from the temporary in the initialization expression. (I doubt that there are any compiles which don't elide the temporary in this case, however.) – James Kanze Aug 28 '12 at 09:58

4 Answers4

3

Where ever the compiler wants to put it, as long as it is destructed at the end of the full expression. Typically, it will be on the stack of a compiler generated initialization function, which is called before main. (Actually, in this particular case, the compiler will typically optimize the temporary away, so it won't be anywhere.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
3

Although, It has been aptly answered by "James", I just want to show you how shall how that shall happen in case of g++

here is some analogous code for the purpose of answer:

[Fooo@EXP]$ cat TestStatic.cpp
#include<iostream>
#include<map>
using namespace std;
class DummyStatic
{
#ifdef TEST
        static map<int,int> mymap;
#else
        map<int,int> mymap;
#endif
};
#ifdef TEST
        map<int,int> DummyStatic::mymap = map<int,int>();
#else
        //Do Nothing
#endif
int main(){
        DummyStatic obj;
}

now when we compile with 'TEST' undefine & see the size of the 'exe' this is what we get

[Fooo@EXP]$ g++ -o TestStatic TestStatic.cpp
[Fooo@EXP]$ size TestStatic
   text    data     bss     dec     hex filename
   2724     300      12    3036     bdc TestStatic

Now we do it with 'TEST' Define ...

[Fooo@EXP]$ g++ -o TestStatic TestStatic.cpp -D TEST
[Fooo@EXP]$ size TestStatic
   text    data     bss     dec     hex filename
   2616     300      36    2952     b88 TestStatic

Observe the difference in BSS

hope this answers your question.

AnotherDeveloper
  • 2,161
  • 2
  • 23
  • 27
2

I assume that you're asking about map<int, int>() temporary.

There's no fundamental difference from the case when you initialize a local variable

int main()
{
    map<int, int> a_map = map<int, int>();
...
}

The temporary is created on the stack of main function.

When a static object which requires non-trivial initialization is created, a compiler generates a function which executes before main. The temporary would be created on the stack of that function (if not optimized away).

Andriy
  • 8,486
  • 3
  • 27
  • 51
0

If I got you right, you are asking about how the static variable is stored. A static variable is shared by all instances of the class, but it's not stored "in" the class, and it's not a "part" of the class. Actually the way static variables are allocated it's not explicitly stated in the standard, so they may be implemented in different ways as long as they behave according to the standard (like being available without an object).

The behavior of static class members is really close to that of global variables. You may think of it (as an abstraction, I do not intend that it is the way it really works!) as if there was a global variable somewhere, and each class member had a pointer to that global.

Consider reading this and this questions on SO.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105