Summary and analysis of four kinds of object lifetime and scope and static usage in C++

  • 2020-04-02 01:31:34
  • OfStack

One, four object lifetime and scope


The stack object
Implicit call constructor (no explicit call in the program)

The heap object
Implicit call to the constructor (no explicit call in the program), to be explicitly released

Global object, static global object

The construction of the global object precedes the main function

Initialized global variables or static global objects are stored in the.data segment

Uninitialized global variables or static global objects are stored in the.bss segment

Static local object

The initialized static local variables are stored in the. Data segment

Uninitialized static local variables are stored in the.bss segment


#include <iostream>
using namespace std;
class Test
{
public:
    Test(int n) : n_(n)
    {
        cout << "Test " << n_ << " ..." << endl;
    }
    ~Test()
    {
        cout << "~Test " << n_ << " ..." << endl;
    }
private:
    int n_;
};
int n;          //An uninitialized global variable with an initial value of 0. N is stored in the.bss segment. (block started by symbol)
int n2 = 100;   //Global variable initialized with an initial value of 100. N2 is stored in the.data segment.
Test g(100);        //The construction of the global object precedes the main function
static Test g2(200);
int main(void)
{
    cout << "Entering main ..." << endl;
    Test t(10);     //Objects created on the stack are automatically released at the end of their lifetime
    {
        Test t(20);
    }
    {
        Test *t3 = new Test(30);        //Objects created on the heap are explicitly released
        delete t3;
    }
    {
        static int n3;          //N3 is stored in the. BSS segment & NBSP; & have spent & have spent & have spent & have spent (compile-time initialization)
        static int n4 = 100;    //N4 is stored in the.data section (compile-time initialization)
        static Test t4(333);    //T4 object runtime initialization & NBSP; & have spent .data section
    }
    cout << "Exiting main ..." << endl;
}



Summary of static usage
1. Used to modify variables inside functions, that is, static variables inside functions. The lifetime of this variable is longer than that of the function, giving the function a certain "state". Functions that use static variables are generally non-reentrant and not thread-safe, such as strtok(3).

2. Used at the file level (outside of the function body) to modify a variable or function, indicating that the variable or function is only visible in this file and cannot be seen or accessed by other files. The professional expression is "having internal linkage" (in short: not exposed to another translation unit).

These two USES of the C language are clear and generally not confusing.


With the introduction of classes in C++, there are two new USES for the static keyword while maintaining compatibility with C:
3. Modifies the data members of a class, known as "static members." The lifetime of this data member is greater than that of the class object (instance /instance). Static data members have one copy per class, and regular data members have one copy per instance.

4. Modifies a member function of a class, called a static member function. Such member functions can only access static members and other static member functions, and cannot access non-static members and non-static member functions.


Related articles: