When do basic C ++ types have an unknown initial value?

When does a basic C ++ type, such as int or float , have an unknown initial value?

How does the memory allocation factor, if at all? How about a declaration? What if it is a member of class / struct / union ? Is C ++ 11 different from C ++ 03 or C ++ 98?

I have my suspicions, but I donโ€™t know if my knowledge is complete (or rather, for that matter)

+4
source share
2 answers

Any PODs (including all major types) will have an unknown value if both:

  • it does not have a static memory allocation (instead, it is created on the stack or with new )
  • it is not initialized, including empty initializations and / or constructor initialization lists

Global / static variables of all types are set to zero as part of the startup process before calling main . Constructors are called for types that have constructors prior to main 1 .

Everything that is not initialized in the constructor is also unknown.

Edit: to clarify, std::string is a good example of a "constructor that doesn't initialize everything" - if you have a local std::string str; , then str will have a certain "empty string" "content, but the contents of the actual buffer or even what the buffer indicates cannot be set to anything meaningful - since the implementation can determine based on length [or in some other way], whether there is an available buffer or more than once we start using a string to store things].

Edit2: As the comment explains, you can also have โ€œhybridโ€ cases where parts of the structure are initialized, for example. a struct that contains some elements of "simple data" and some elements that have constructors. Those who have constructors will have their own constructor. Simple data will not be initialized.

1 It may happen that the constructors created by the code are part of or called from within the main function, but if this is the case, it will be "before any of your code is launched in the main".

+7
source

In the section "Working draft C ++, 2012-11-02"

3.6.2 Initialization of non-local variables [basic.start.init]
2 Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) must be initialized with zeros (8.5) before any other initialization.

Variables with static storage are initialized with at least zero.

3.7.3 Automatic storage duration [basic.stc.auto]
2 [Note. These variables are initialized and destroyed as described in 6.7. - final note]

6.7 says nothing about how automatic variables are initialized.

3.7.4 Dynamic storage duration [basic.stc.dynamic]
...
3.7.4.1 Distribution Functions [basic.stc.dynamic.allocation]
... There are no restrictions on the contents of the allocated storage when returning from the distribution function. The order, contact, and initial value of the vault allocated by successive calls are not specified by the distribution function.

8.5 Initializers [dcl.init]
7 By default, an object of type T is initialized:
- if T is a (possibly cv-qualified) type of class (section 9), the default constructor for T is called (and initialization is poorly formed if T does not have an available default constructor); - if T is an array type, each element is initialized by default;
- otherwise, initialization fails.

If you provide an explicit initializer, any variable will have a known value.

If you do not provide an explicit initializer for the POD type, it depends on the storage class. Static or stream variables will be initialized to zeros, while automatic or dynamically allocated variables are not.

If you have a complex type, the same rules apply. If you do not have an explicit initializer, through the constructor (by default) or otherwise, the initial value of the main types depends on the storage class.

Finally, memory allocated through malloc will be uninitialized, while calloc memory will be initialized to zero.

+2
source

All Articles