2

In general, I would like to know where a member's static function's local variables are stored? I.e. if a static variable is only used inside a static function, is the variable only initialized once?

Please refer to the code below

std::string const CONST1 = "const1";
std::string const CONST2 = "const2";
std::string const CONST3 = "const3";

class Test
{
  public:
    static const std::vector<std::string> GetSomeMap();
}

const std::vector<std::string> Test::GetSomeMap()
{
  static std::vector<std::string> SomeMap = boost::assign::list_of(CONST1)(CONST2)(CONST3);
  
  return SomeMap;
}

With the above code, is there an advantage to declaring SomeMap as static? (I am expecting it to be only initialized once.)

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
BubbleBoy
  • 83
  • 5
  • 2
    It is initialised once, but note that you return a copy of the map instead of a reference to it. You might want to fix that. – Blacktempel May 12 '16 at 05:15
  • can we assume that that RVO ( Return Value Optimization) will not be performed ? – BubbleBoy May 12 '16 at 06:26
  • Sometimes class writers avoid using a static member variable by replacing it with a static local variable inside a static member function (but the function needs to return a reference or pointer, unlike in your example). This is done either to avoid probelms with initialization across multiple translation units, or to avoid the requirement that the static member be initialized inside an implementation file (for a header-only class definition). – Christopher Oicles May 12 '16 at 10:35
  • Note that the two uses of `static` here have completely different meanings. Don't look for parallels between them; pretend that they are different words. – Pete Becker May 12 '16 at 11:26

4 Answers4

5

if a Static variable is used inside a static function is the variable only initialised once ?

The answer is "yes".
It is also "yes" for static variables of regular, i.e. non-static, member functions.
It is also "yes" for static variables of non-member functions.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
3

Static variables inside functions (regardless of the type of function) are stored in the "DATA" segment, just like global variables. So you could say that in this way, function static variables are similar to global ones, just that they are only accessible by name within a limited scope (the function body).

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

If a variable is static ,it is stored in heap. If a variable is a member of static function, it is stored in static local variable. And, they are only initialised once.

LuChen
  • 1
  • 2
0

In General I would like to ask where a Member Static function's local variables are stored ?

Depends. A static constant plain old datatype may be stored in a read-only data segment. A static variable with constant initializer may be stored in the data segment, and a static variable that requires dynamic initialization may be stored in the BSS segment.

if a Static variable is used inside a static function is the variable only initialised once ?

Yes. In this case, SomeMap will be initialized the first time control passes through its declaration.

The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any other initialization takes place. A local object of POD type (3.9) with static storage duration initialized with constant-expressions is initialized before its block is first entered. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope (3.6.2). Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration.

Section 6.7, paragraph 4 of ISO/IEC 14882:2003(E) (sorry, I don't have a more recent copy of the standard handy)

From the above code, is there an advantage of declaring SomeMap as static ?( I expect it to be only initialized once )

Yes there is an advantage to declaring it static -- it will only be initialized once and only initialized if it is used. If Test::GetSomeMap is never call, SomeMap is never initialized.

As @Blacktempel states above, however, Test::GetSomeMap should return by reference to remove any doubts about the creation of extra copies SomeMap.

You should also note that you are incurring the cost of the creation of three strings (CONST1, CONST2, and CONST3), each of which may allocate heap memory to store the a copy of their constant-expression character string initializers ("const1", "const2", "const3"). Furthermore, if you call Test::GetSomeMap, you are also incurring the cost of initializing the SomeMap vector which may also allocate heap memory to store the copies of the strings.

If you are concerned about memory usage and initialization overhead and you truly want a static constant array of strings, just declare one, like so:

   static const char* const * GetSomeMap(void) {
    static const char* const SomeMap[] = {"const1", "const2", "const3"};
    return SomeMap;
}

SomeMap will consume a minimum of memory with no initialization overhead (and be completely unchangeable).