C ++ static variable memory allocation, compilation stage, decryption

Source: Internet
Author: User
Tags variable scope

Note:

C ++ static member variables are member variables modified with static, not part of the object but part of the class. Therefore, static member variables can be used when no object is instantiated. But it must be initialized.

It must be initialized because: Global static variables are automatically initialized, but local variables


Because static variables can only be initialized once, do not place the initialization member variables in the following places: 1. class constructor may be called multiple times); 2. the header file in the header file may be contained in multiple places or executed multiple times ). It should be put in the application and initialized anywhere outside the class, for example, in main, global function, or outside of any function:


All objects (not just static objects) are initialized only once.
Because a has never been destroyed after it is initialized as 1 (because it is static), it will not be initialized again


There are three memory allocation methods:
1) distributed from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.
2) create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.
3) distributed from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but the problem is also the most.

The static member variables of the class are the same as the static variables in the function. The memory is allocated during compilation and will not be released until the program exits. It is not released with the deletion of the object:

Why must the static member variables of the class be initialized outside the class?


My understanding: Because static variables must be initialized during compilation, the initial values of static or non-static variables of global variables can be assigned 0. The class variables must be initialized using constructors, but no object is created during compilation, so no constructor is run. Therefore, static variables of the class are not initialized during compilation. Therefore, you need to initialize the static variable before the main outside the class, no matter whether the static variable scope is private or public, because private does not affect during compilation. However, the private variable in the class cannot be called once it is running.



It can be called through an object or through a class.

Class
{
Public:
Int I; // There are default values
};
Class B
{
Public:
Static int n;
Static A Aobj;
};
Int B: n = 1; // initialization of static member variables
A B: Aobj; // static member variable initialization (instantiation)
Void main ()
{
B Bobj;
Printf ("B: n = % d Bobj. n = % d Bobj. aobj. I = % d/n ", B: n, Bobj. n, Bobj. aobj. I );
}

Private Static member variables are also initialized outside the class, which seems to be inconsistent with its private attributes.

The following tests show an interesting phenomenon.

Class B
{
Staticint I;
Public:
B () {I = 3 ;}; // comment out the line and change the output to 2.
Int p () {return I ;};
};
Int B: I = 2; // comment out this line and the compilation reports an error.
Void main ()
{
B Bobj;
Printf ("private: static int B: I = % d/n", Bobj. p (); // output 3
}

1. "In this case. the row initialized outside the class does not have initialization function at all. the value assigned by it is useless. (This is true for private static member variables. public or useful) "This sentence is wrong !!!

It does not affect whether the static member variable is private data, because when the initial value of the static member variable is set, it is not subject to any access permissions,

The assignment of static variables is in the compilation phase, so it is earlier than the constructor. When the class declares an object, I = 3 overwrites the previous value. So it is 3.

2. When B () {I = 3 ;}; is injected, the value assignment in the constructor is not overwritten. Therefore, the result is 2.

3. Because static variables can only be initialized once, they are placed in external or global functions of the function.


However, it should be noted that the type of static member variables should appear in the initialization statement, because this is an initialization operation, not a value assignment operation. Static member variables are defined only during initialization rather than during class declaration. If no initialization operation is performed, a link error occurs.

This article is from the "energy of Combustion Technology" blog, please be sure to keep this source http://boyishachang.blog.51cto.com/3485129/1285956

Related Article

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.