Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of C++ static persistence variable

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report