Initialization of A/C + + variable

Source: Internet
Author: User
1. Variables declared in the global domain are automatically initialized to 0, such as:

Double salary;
int day;

2. If the variable is defined in a local domain, or is dynamically assigned through a new expression,
The system does not provide it with an initial value of 0, which is considered uninitialized and has a random value.
Class objects are automatically initialized by default constructors

3. C + + supports two forms of initialization:
1: Use the display syntax form of the assignment operator. such as: int ival=1024;
2: Implicit form, the initial value is placed in parentheses. such as: int ival (1024);

4. Arrays can be initialized with a set of numbers, such as:
const int aray_size=3;
int ia[aray_size]={0,1,2};

int a[5] ={0}; Then each element of a is initialized to 0

If the specified number of dimensions is greater than the number of the given element, the element that is not initialized will be placed to 0. You can also not specify dimension values, such as: Int ia[]={0,1,2};

5. Initialization of global variables
For global variables of different compilation units, the order of initialization is not guaranteed, so it is unwise to establish dependencies between their initialization order for global variables in different compilation units.
In addition, there is no way to catch the exception thrown by global variable initialization, in general to reduce the use of global variables, especially those requiring complex initialization of global variables. So:1, try not to use global variables
2, with static variables, access through the accessor for example: global variables
int a = 5;
int b = A;
If a, and b are defined in the same file, that's fine, and the result is B equals 5.
If A and B are defined in different files, B is not guaranteed to be equal to 5, which means that a is not guaranteed to initialize first. In fact, the initialization order between other global or static variables is not guaranteed, except that the initialization of global objects defined in the same file is done in a defined order. The solution to this problem is to use a wrapper function instead of using a global variable directly to access it, for example
int Get_a ()
{
static int a = 5;
return A;
}
int Get_b ()
{
static int b = Get_a ();
return b;
}

In this way, regardless of whether get_a and get_b are defined in the same file, Get_b can always return the correct result, because the static variables inside the function are initialized at the first access time. At any time, if a "nonlocal static object" is defined in a different compiled unit, and the correct behavior of these objects depends on a particular order in which they are initialized, which can cause problems. You have absolutely no control over the initialization order of the partial static objects in the compiled unit. For static objects in a function (that is, "local"        Static objects are initialized when they first encounter the definition of an object during a function call ...    PS: Never write a program that is relevant to the compilation sequence. With regard to the initialization of global variables, C and C + + are different.
In C, a global variable can only be initialized with a constant, or the compiler will complain.
In C + +, if int a = 5 is defined in a file; to define int b = A In another file, a must be declared before: extern int A; if so, int b = A; This sentence is also in two steps: in the compile phase, edit The translator initializes the B as uninitialized data to 0; In the execution phase, there is a global object's construction process before main is executed, int b = A; it is executed as a copy initialization construct of int-type Object B.
In fact, in C + +, the initialization of global objects and variables is independent, and if it is not like int a = 5, then uninitialized data like B is not initialized.
In C + +, the order of the constructor calls of global objects and variables is related to the declaration, that is, the first call declared in the same file. For global objects and variables in different files, their constructor call order is undefined and depends on the specific compiler.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.