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

Case Analysis of C++ non-local static data

2025-03-31 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 "C++ non-local static data case analysis". 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!

Static data include:

Namespace variable √ defined in namespace

The class domain variable √ declared as static in the class

The local static variable × declared as static in the function

Global variables defined in the file (with or without static decorations) √

The non-local static data mentioned above refers to cases 1, 2, and 4 except for the third case.

The compilation unit refers to .o files. If a project consists of n separate cpp and corresponding header files, it will be pre-compiled to generate n .o files. Sometimes we call these * .o files as object files, which are also called compilation units as the final unified executable files.

To sum up, the meaning of the title of this paper is: if multiple static data (without local variables) are defined in multiple files, then the interdependence between them will be in a subtle dilemma.

What dilemma? The thing is, because static data is initialized at the beginning of the program (whether specified initialization or automatic initialization of the system), and the C++ standard does not specify the initialization order of these static data in multiple files, this will lead to a problem: if non-local static data depend on each other, the running result of the program can not be predicted because of the uncertainty of initialization order.

For example, programmer Jack developed a super-useful class called car and defined an object of this class for others to use.

Class car / / closed source code {... public: void startup (params);...}; extern car BMW; / / A high-performance car ^ _ _ ^

On the other hand, at different times and places, different programmers, Rose, have developed a logistics MF for different purposes, which will naturally use Jack's car objects to accomplish some work.

Class MF {public: MF (params);...}; MF::MF (params) {. BMW.startup (); / / use car object}

Soon, the code for Rose can be disastrous, because C++ compiles with no guarantee that the car object BMW will be initialized when the MF object is initialized. Therefore, it is very likely that MF called an uninitialized object's startup function, which is embarrassing.

To avoid this situation, it is also simple to define a function specifically to deal with the non-local static data in these troublesome multiple compilation units. For example:

Car & BMW () {static car c; / / Local static object c return c;} at this point, Rose's use of car objects requires only one small change: MF::MF (params) {. BMW () .startup (); / / use car object}

Yes, there is a pair of parentheses after the BMW. On the whole, the process of user Rose using the car object is exactly the same, but the logic of the program is quite different. When Rose first calls the function BMW, the local static object c is created and initialized, which ensures the correctness of calling the startup () function. Secondly, if startup () has not been called once, then the local static object c will not be generated at all! Perfect! Through this design, we solve two problems with a backhand hook at the same time: it ensures the order of initialization and improves the performance of the program.

This is the end of the content of "C++ non-local static data example Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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