5

I have the following

char mem_pool[1024*1024*64]; 

int main() {
    // ... 
}

I'm trying to get a thorough understanding how will mem_pool be initialized. After a lot of search my conclusions are :

  • it's a static initialization (not as in static the keyword, but as in "run before the program does - during static initialization phase")
  • it will run in 2 phases : zero initialization and default initialization (the second phase won't do anytning)
  • it's an array of PODs so the default initialization for every element should apply, but due to the previous 2 points we won't have an array of indeterminable values (as we would with a char ar[N] in a function scope) but an array of zeroes.

Could someone help me dissambiguate what's guaranteed by the language and correct me if I'm wrong ?

I also thought of doing any of the following

char mem_pool[1024*1024*64] {}; 
char mem_pool[1024*1024*64] = ""; 

I suspect it's a better/recommended practice, but for now I need to understand my initial question.

Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
  • Your observations are correct. Did you need something else on top of a verification? – Lightness Races in Orbit Mar 17 '15 at 21:07
  • **1**. Correct **2**. No. zero intialization and default initialization are different type, not different phases **3**. No. char is not a POD. It is a built in type. **Conclusion** No you don't need to do any of those two. An array outside main function is zero initialized. – SwiftMango Mar 17 '15 at 21:09
  • 1
    @texasbruce: 2. _is_ correct. The two phases are static initialisation (which involves zero-initialisation) and then normal initialisation (which is where the default-initialization comes in). I think you just misread it. – Lightness Races in Orbit Mar 17 '15 at 21:10
  • Apparently I missed initialization class. Anyone have the spec reference so I can study? Nevermind, just found: http://stackoverflow.com/questions/15212261/default-initialization-of-pod-types-in-c – Matthew Moss Mar 17 '15 at 21:10
  • 3
    @MatthewMoss: I could put together some quotes but, honestly, this has been done to death on SO. Find some of our higher-voted questions about initialisation and get reading. – Lightness Races in Orbit Mar 17 '15 at 21:11
  • @LightnessRacesinOrbit maybe standard reference ? I'm trying to follow [this](http://en.cppreference.com/w/cpp/language/default_initialization) (and its links in the site, which has been most helpful to me) and I have to perform mental leaps to get to that conclusion (I mean it's never stated clearly) In any case verification is good enough for me, I've been reading quotes for the last half hour – Lorah Attkins Mar 17 '15 at 21:12
  • 1
    @Norah: There you go. – Lightness Races in Orbit Mar 17 '15 at 21:18
  • 2
    @texasbruce: 3. `char` most certainly _is_ a POD type [ref. C++11 3.9/9, 3.9.1/8 & 3.9.1/7] – Lightness Races in Orbit Mar 17 '15 at 21:28

1 Answers1

5

Your understanding is correct.

The array's elements will all be zero-initialised, because the array has static storage duration:

[C++11: 3.6.2/2]: Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place. [..]

[C++11: 8.5/5]: To zero-initialize an object or reference of type T means:

  • if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;
  • if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;
  • if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zero-initialized and padding is initialized to zero bits;
  • if T is an array type, each element is zero-initialized;
  • if T is a reference type, no initialization is performed.

If it did not have static storage duration, the elements would all have indeterminate values:

[C++11: 8.5/11]: If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [..]

[C++11: 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055