<p>Is global memory initialized in C++? And if so, how?</p>

<p>(Second) clarification:</p>

<p>When a program starts up, what is in the memory space which will become global memory, prior to primitives being initialized? I'm trying to understand if it is zeroed out, or garbage for example.</p>

<p>The situation is: can a singleton reference be set - via an <code>instance()</code> call, prior to its initialization:</p>

<pre><code>MySingleton* MySingleton::_instance = NULL;
</code></pre>

<p>and get two singleton instances as a result?</p>

<p>See my C++ quiz on on multiple instances of a singleton...</p>

[{'answer_id': 60655, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 4, 'selected': True, 'text': "<p>Yes global primitives are initialized to NULL.</p>\n\n<p>Example:</p>\n\n<pre><code>int x;\n\nint main(int argc, char**argv)\n{\n  assert(x == 0);\n  int y;\n  //assert(y == 0); &lt;-- wrong can't assume this.\n}\n</code></pre>\n\n<p>You cannot make any assumptions about classes, structs, arrays, blocks of memory on the heap...</p>\n\n<p>It's safest just to always initialize everything. </p>\n"}, {'answer_id': 60669, 'author': 'JeffV', 'author_id': 445087, 'author_profile': 'https://Stackoverflow.com/users/445087', 'pm_score': 3, 'selected': False, 'text': "<p>Coming from the embedded world...</p>\n\n<p>Your code gets compiled into three types of memory:<br>\n   1. .data: initialized memory<br>\n   2. .text: constants and code<br>\n   3. .bss: uninitialized memory  (initialized to 0 in C++ if not explicitly initialized)</p>\n\n<p>Globals go in .data if initialized. If not they are placed in .bss and zero'ed in premain code.</p>\n"}, {'answer_id': 60677, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 0, 'selected': False, 'text': '<p>Variables declared with static/global scope are always initialized under VC++ at least.</p>\n\n<p>Under some circumstances there can actually be a difference in behaviour between:</p>\n\n<pre><code>int x = 0;\n\nint main() { ... }\n</code></pre>\n\n<p>and</p>\n\n<pre><code>int x;\n\nint main() { ... }\n</code></pre>\n\n<p>If you are using shared data segments then VC++ at least uses the presence of an explicit initialization along with a <code>#pragma data_seg</code> to determine whether a particular variable should go in the shared data segment or the private data segment for a process.</p>\n\n<p>For added fun consider what happens if you have a static C++ object with constructor/destructor declared in a shared data segment.  The constructor/destructor is called every time the exe/dll attaches to the data segment which is almost certainly not what you want.</p>\n\n<p>More details in this <a href="http://support.microsoft.com/kb/125677" rel="nofollow noreferrer">KB article</a></p>\n'}, {'answer_id': 60707, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 4, 'selected': False, 'text': '<p>From the standard:</p>\n\n<blockquote>\n  <p>Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called <em>static initialization</em>; all other initialization is <em>dynamic initialization</em>. Objects of POD [plain old data] types (3.9) with static storage duration initialized with constant expressions (5.19) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit. [Note:8.5.1 describes the order in which aggregate members are initialized. The initial- \n  ization of local static objects is described in 6.7.] </p>\n</blockquote>\n\n<p>So yes, globals which have static storage duration will be initialized.  Globals allocated, e.g., on the heap will of course not be initialized automatically.</p>\n'}]
['https://Stackoverflow.com/questions/60653', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2167252/']