Influence of static keywords on Object Storage Period and link Period

Source: Internet
Author: User

Before reading this article, I first asked myself a question: can a name with the static keyword have an external link period? If you already have a definite answer, and the conclusion is the same as that in this article, congratulations, you don't have to read this article, because you know what I will talk about.

Storage Period of affected objects

Storage Period, also known as storage duration, refers to the life cycle of the memory space occupied by the variable. That is to say, it determines the time period that the space must exist during the execution of the entire program. It is greater than or equal to the lifetime of the object. The storage period defined in C ++ 03 includes static (static storage period), dynamic (dynamic storage period), and automatic storage period (automatic storage period ).

The effect of static keywords on the object storage period is mainly reflected in local variables.

void f(){    // Automatic storage duration    int i = 10;    // Static storage duration    static int j = 10;}

VariableIHas an automatic storage period, that is, storageIFrom the left brackets{Start with brackets}End. (Note: most compilers allocate the required stack space for the used local variables by adding or subtracting the stack pointer register (SP/ESP/RSP) at the beginning of a local block. Therefore, the actual{It starts to exist, not the definition .) In contrast, static variables are added.JWith a static storage period, its space exists from the program loading until the end of the program.

Link period of affected objects

The link period, also known as linkage, controls the visibility of objects. It differs from scope: Scope determines the visibility of this object in the same compilation unit (including the source file after the header file. The link period determines the object visibility across multiple compilation units. The connection period defined in C ++ 03 includes external (external link period), internal (internal link period), and no (no link period ).

The effect of static keywords on Object link time is mainly reflected in global variables and functions.

// A.cpp// External linkage by defaultint i = 10;// Internal linkagestatic int j = 10;// B.cppextern int i;extern int j;int main(){    int x = i;    // Link eror below! Unresolved external symbol "j".     // int y = j;}

By default, global variables have an external link period, that is, global visibility-visible in other compilation units (B. cpp. After static is added, it becomes an internal link period. Therefore, the linker reports an error when accessing other files.

The function is the same. Only the compilation unit of the current compilation unit can call the function with the static function added.

If you know what I have said so far, you may be surprised at the following. Static can also enable the name to have an external link period!

// namespace scope class// MyClass.hclass MyClass{public:    // No linkage    int m_i;    // External linkage!    static int m_j;};// MyClass.cpp#include "MyClass.h"int MyClass::m_j = 10;// Another.cpp#include "MyClass.h"int main(){    MyClass::m_j = 20;    // Compile error below!    // m_i = 1;    // MyClass::m_i = 1;}

Here, common member variables do not have any link period, because they cannot be directly accessed outside of its scope (Class scope) by name m_ I. However, static member variables have an external link period, so they can be accessed directly in other compilation units, provided that the class name is limited (::).

Finally, by the way, a member function has an external link period no matter it is added without static.

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.