Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is global memory initialized in C++?

Is global memory initialized in C++? And if so, how?

(Second) clarification:

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.

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

MySingleton* MySingleton::_instance = NULL;

and get two singleton instances as a result?

See my C++ quiz on on multiple instances of a singleton...

like image 567
theschmitzer Avatar asked Sep 13 '08 16:09

theschmitzer


People also ask

Are global variables initialized in C?

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.

Are C globals zero initialized?

Yes, all members of a are guaranteed to be initialised to 0. 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.

What is the initial value of global variable in C?

Thus global and static variables have '0' as their default values. Whereas auto variables are stored on the stack, and they do not have a fixed memory location.

Are global variables initialized at compile time?

In an ideal world all static variables are const-initialized. If the initial value of a static variable can't be evaluated at compile time, the compiler will perform zero-initialization.

How to initialize static and global variables in C++?

In C, static and global variables are initialized by the compiler itself. Therefore, they must be initialized with a constant value. Note that the above programs compile and run fine in C++, and produce the output as 10. As an exercise, predict the output of following program in both C and C++. This article is contributed by Shankar Shastri.

What are global variables in C++?

"Global variables" are defined at file scope, outside any function. All variables that are defined at file scope and all variables that are declared with the keyword static have something called static storage duration. This means that they will be allocated in a separate part of the memory and exist throughout the whole lifetime of the program.

What is a typical memory representation of C program?

A typical memory representation of C program consists of following sections. 1. Text segment 2. Initialized data segment 3. Uninitialized data segment 4. Stack 5. Heap 1. Text Segment:

Where is the global string stored in C?

For instance, the global string defined by char s [] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in the initialized read-write area.


2 Answers

From the standard:

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 static initialization; all other initialization is dynamic initialization. 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- ization of local static objects is described in 6.7.]

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.

like image 84
Derek Park Avatar answered Sep 23 '22 05:09

Derek Park


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.

like image 37
Brian R. Bondy Avatar answered Sep 23 '22 05:09

Brian R. Bondy