Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of C++ static persistence variable

Shulou Source: shulou.com Published: 2022-06-02 09:52:18 09月29日 Update

This article introduces the relevant knowledge of "example Analysis of C++ static continuous variables". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Auto keyword

We now know that using the auto keyword can automatically deduce variable types, especially in very complex situations, and using auto can greatly simplify the code. But in the early C++ and C languages, the auto keyword was used to explicitly indicate that a variable was automatically stored.

Auto float dis

This feature is rarely used, so it has been updated in Category 11 to give it a new meaning.

2. Static persistent variable

In addition to automatically storing variables, there are static persistent variables in C++. The definition of static persistent variable C++ and C language is the same, it has three kinds of links, namely, external connectivity, internal connectivity and non-linkage. Among them, external linking refers to being accessible in other files, internal linking means that it can only be accessed in the current file, and unlinking refers to being accessible only in the current function or code block.

Although they have different ranges, they all exist throughout the program, so they have a longer life, and because the number of static persistent variables remains the same during the program, there is no need to use stacks to manage them. The compiler will allocate fixed blocks of memory to store all static variables that remain throughout the program.

And all static variables that are not explicitly initialized will be set to 0 by the compiler. By default, static arrays and structures set all bits of all elements and members to 0.

Here's how to create these three static persistence variables:

Int cnt = 1000; static int one_file = 50; void func1 (int n) {static int ret = 0;} int main () {/ / some statements}

In the above code, we define three static persistent variables: cnt,one_file,ret. Where cnt is externally linked, it can be used anywhere. One_file is internally linked and can be used anywhere in the current file. Ret is not linked and can only be used in the function func1.

But one thing to note here is that although ret can only be used in the function func1, it doesn't mean that ret does not exist before the function func1 is executed. As mentioned earlier, static persistent variables have separate blocks of memory to store and are not affected by the life cycle of the function.

Let's look at an example:

Void test () {static int ret = 0; ret++; cout

Tags: Variable static link function key keyword clocked code storage run meaning situation file is in program analysis memory content cycle place Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL OPPO Reno Shulou Tech Info Docker