For automatic initialization of built-in variables
Code Listing 1:
#include <stdio.h>#defineCONST 100int*P1;inta[2];intb;Static intC;main () {intD; Static inte; intf[2]; int*P2; printf ("const=%d\n", CONST); printf ("a[0]=%d\n", a[0]); //printf ("*p1=%d\n", *P1);printf"b=%d\n", B); printf ("c=%d\n", c); printf ("d=%d\n", D); printf ("e=%d\n", E); printf ("f[0]=%d\n", f[0]); printf ("*p2=%d\n",*p2);}Press CTRL + C to copy the code
Output:
const=100
A[0]=0
B=0
C=0
d=2514932
E=0
f[0]=1307813
*p2=457819009
The data area holds the initialized global variables, static variables (both global and local), constants.
Uninitialized data area (Uninitializeddata SEGMENT,BSS) holds globally uninitialized variables. BSS data is initialized to 0 or null before the program starts executing. The variable in the BSS segment occupies only one sign bit in the target file, and the compiler does not allocate space for the variable, so so-called "initialize to 0" refers to the request for space at the link stage and is initialized to 0. Only the initialized global variables are to occupy the size of the target file.
In other words global variables, static variables (both global and local), constants not explicitly initialized are initialized by default when 0 or null.
If you try to print *p1, a segment error occurs because P1 points to a null address.
While a local non-static variable is not explicitly initialized when it is a random number, it is generally a large number.
Automatic initialization for class-type variables
A class-type variable invokes the default constructor for initialization, whether it is a global or local scope.
The so-called "default constructor" is the constructor that refers to an empty argument.
Code Listing 2:
class a{public: int value; A () { cout<<"intitialize a"<<Endl; Value=3; }}; A A1; int Main () { A A2; cout<<a1.value<<Endl; cout<<a2.value<<Endl; return 0 ;}
Output:
Intitialize A
Intitialize A
3
3
If a class does not explicitly define any constructors , the compiler automatically generates an empty argument for it, called the composite default constructor. The composition default constructor initializes members with 3 rules:
1. When an object is in a global scope or is a static local object, the built-in member variable of the class is initialized to 0.
2. When an object is defined in a local scope, the built-in member variable of the class is not initialized to 0.
Code Listing 3:
class a{public: int value;}; A A1; int Main () { A A2; Static A A3; cout<<a1.value<<Endl; cout<<a2.value<<Endl; cout<<a3.value<<Endl; return 0 ;}
Output:
0
2510836
0
3. Initializes a class-type member according to its own (composite) default constructor . -Important
Code Listing 4:
classa{ Public: intvalue; A () {Value=5; }};classb{ Public: intvalue; A; }; B B1;intMain () {B b2; cout<<b1.value<<"\ t"<<b1.a.value<<Endl; cout<<b2.value<<"\ t"<<b2.a.value<<Endl; return 0;}
Output:
0 5
134514784 5
Code Listing 5:
classa{ Public: intvalue;};classb{ Public: intvalue; A;}; B B1;intMain () {B b2; cout<<b1.value<<"\ t"<<b1.a.value<<Endl; cout<<b2.value<<"\ t"<<b2.a.value<<Endl; return 0;}
Output:
0 0
134514736-1081710584
If the class explicitly provides a constructor with parameters, the compiler will no longer generate null arguments for it's constructor. At this point, the cannot define a class type variable with an empty argument . The following code is wrong:
class a{ public : int value; a ( int i): value (i) {} }; class b{ public : int value; A a;//calls the constructor of an empty argument}; int main () {a A; return 0 ;}
Transferred from: http://www.cnblogs.com/zhangchaoyang/articles/2671551.html
Automatic initialization of C + + variable--important