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

What is the role of C++ static and what is the difference between global and local variables

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

Share

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

This article will explain in detail what the role of C++ static is and what is the difference between global variables and local variables. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The role of 1.static

(1) when modifying the local variable, it is initialized only once, which prolongs the life cycle of the local variable and is not released until the end of the program.

(2) modify global variables, global variables can be accessed intelligently in this file, but not in other files.

(3) decorating functions are similar to decorating global variables, which can only be called in this file, not in other files.

(4) when you do not want to be released, you can modify it with static.

(5) consider data security, such as static should be given priority when the program needs to use global variables.

What is the difference between 2.staic global variables and local variables, where to store them? Difference:

Static global variables:

(1) the static local variables behind the static variable package allocate memory in the global data area.

(2) uninitialized static global variables are automatically initialized to 0.

(3) the static global variable is visible in the entire file in which it is declared, and it is not visible outside this file, let alone available. (therefore, the advantage is that variables with the same name can be defined in other files without conflicts. )

Static local variables:

(1) same as static global variable (1).

(2) the static local variable is initialized for the first time when the program executes to the object declaration, and then the function call is no longer initialized.

(3) static local variables are generally initialized at the declaration. If initialization is not displayed, the program will automatically initialize to 0. 0.

(4) static local variables always reside in the global data area until the end of the program. But its scope is local scope, and when its defined statements and functions end, its scope also ends.

Static-decorated variables are stored in the static variable area of the global data area, including global static variables and local static variables, and all allocate memory in the global data area. It is automatically initialized to 0 when initialized.

Let's explain it in a bottom-up way:

1. Code snippet: mainly the code of the program and the libraries statically linked at compile time. This memory size is determined before the program runs, and it is read-only, and there may be some constants, such as string constants.

two。 Data segment: it is divided into data and bss segments, which are represented as static memory segments. Data segments store initialized global variables (static memory allocated variables and initialized global variables). The bss segment stores uninitialized global variables, and the bss segment is cleared in memory.

3. Heap segment: used for program dynamic memory allocation and management, how to allocate and manage is decided by the developer of the program, the size is not fixed (related to your machine memory), can be dynamically scaled.

4. Mapping segment: this memory area stores vectors linked to other dynamic libraries, shared memory mapping vectors, and so on.

5. Stack segment: the stack is a first-in-and-out data structure, which is managed by the program itself, such as saving and deleting local variables, function calls and so on.

6. Input environment variables and parameter segments: environment variables when the main memory program is executed, input parameters, and so on.

7. Is the system area.

About the role of C++ static and what is the difference between global variables and local variables to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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