Non-local static object initialization order uncertainty

Source: Internet
Author: User

Is there such a blog https://www.ibm.com/developerworks/community/blogs/12bb75c9-dfec-42f5-8b55-b669cc56ad76/entry/_e8_b7_a8c__e6_96_87_e4_bb_b6_e5_92_8c_e5_ba_93_e5_af_b9_e9_9d_99_e6_80_81_e5_af_b9_e8_b1_a1_e8_bf_9b_e8_a1_8c_e5_88_9d_e5_a7_8b_e5_8c_965 on IBM China Development blog? Lang = ZH

The compiler cannot guarantee the initialization sequence of non-local static object initialization order across multiple files or libraries. Therefore, the non-local static object initialization order is used to initialize other objects, this causes other objects to be initialized as junk data in the memory.

This issue is mentioned in Clause 4 "correctly initialized before the object is used" in <valid tive C ++>. The solution is to change the non-local static object to the local static object in the function, and the function returns the reference of this object to use this function for initialization of other objects. This can be done because local-static-object will perform initialization only when this definition is met for the first time and will not be executed in the future. The function ensures that the object used to initialize other objects has been initialized. In this way, we will not see the situations mentioned in the above blog.

Of course, the blog also mentions that you can use the compiler to specify a priority for each module to display the initialization sequence of the specified module (of course not the compilation sequence), but for large projects, after all, it is not an applicable method,ArticleIt's a bit suspected of promoting the xlc compiler...

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.