Is Global Memory Initialized in C++

Is global memory initialized in C++?

Yes global primitives are initialized to NULL.

Example:

int x;

int main(int argc, char**argv)
{
assert(x == 0);
int y;
//assert(y == 0); <-- wrong can't assume this.
}

You cannot make any assumptions about classes, structs, arrays, blocks of memory on the heap...

It's safest just to always initialize everything.

Are global variables always initialized to zero in C?

Yes, all members of a are guaranteed to be initialised to 0.

From section 3.5.7 of the C89 standard

If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant.

Global variable 0-initialized penalty

No, since the C++ (and C) standard says that all global/static variables that are not initialized explicitly by the programmer, must be initialized to zero. Such variables are placed in a special segment called .bss. They are initialized to zero before main() is called.

If you initialize your global/static explicitly, but to the value 0, the compiler is smart enough to realize this and still put it in the bss segment.


You can test this for yourself with an example like this:

#include <stdio.h>

static int uninit;
static int init_zero=0;
static int init_one=1;

int main (void)
{
printf("%p\n", &uninit);
printf("%p\n", &init_zero);
printf("%p\n", &init_one);

return 0;
}

In this example, the uninit and init_zero variables will end up at adjacent memory addresses (likely 4 bytes away from each other), since they are both in the .bss segment. But the init_one variable will end up somewhere else entirely, because it is allocated in the .data segment.

Are the members of a global structure initialized to zero by default in C?

From the C99 standard 6.7.8/10 "Initialization":

If an object that has automatic
storage duration is not initialized
explicitly, its value is
indeterminate. If an object that has
static storage duration is not
initialized explicitly, then:

— if it has pointer type, it is
initialized to a null pointer;

— if
it has arithmetic type, it is
initialized to (positive or unsigned)
zero;

— if it is an aggregate, every
member is initialized (recursively)
according to these rules;

— if it is
a union, the first named member is
initialized (recursively) according to
these rules

Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).

The C++ 2003 standard has a similar requirement (3.6.2 "Initialization of non-local objects"):

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.

Sometime after that zero-initialization takes place, constructors are called (if the object has a constructor) under the somewhat more complicated rules that govern the timing and ordering of those calls.

Why do C and C++ compilers place explicitly initialized and default initialized global variables in different segments?

Neither language C or C++ has any notion of "segments", and not all OSs do either, so your question is inevitably dependent on the platform and compiler.

That said, common implementations will treat initialized vs. uninitialized variables differently. The main difference is that uninitialized (or default 0-initialized) data does not have to be actually saved with the compiled module, but only declared/reserved for later use at run time. In practical "segment" terms, initialized data is saved to disk as part of the binary, while uninitialized data is not, instead it's allocated at startup to satisfy the declared "reservations".

Where do uninitialized Global Variables go after initializing?

When the OS loads your program, it allocates enough storage from your program's address space to store everything in the .bss section and zeros all of that memory. When you assign or read from or take the address of the variable, you're manipulating that memory that was allocated to provide storage for the .bss section.



Related Topics



Leave a reply



Submit