The order of C + + static initialization

Source: Internet
Author: User

This two-day project code encountered a very confused problem, the problem can be described as: a static member initialization of the direct core drop, the static member is initialized by a static member in another file to complete . The problem also occurs on the global object. The problem can be described as: the static initialization order of variables for today's discussion.

Specific code can be described in the following:

Test1.cpp

#include <string>

STD::string a = "test";

Test2.cpp

#include <iostream>

extern std::string A;

std::string B = A;

int main ()

{

std::cout<<b<<std::endl;

}

When executing the following compile command:

g++-G Test1.cpp test2.cpp

The execution result is correct, output the "test" text, but when the following compile instructions are executed:

g++-G Test2.cpp test1.cpp

The results of the implementation are as follows:

Segmentation fault (core dumped)

To debug the core file, the function frame stack is as follows:

(GDB) bt

#0 0x00007ff5f0932f2b in Std::basic_string<char, Std::char_traits<char>, std::allocator<char>;:: Basic_string (std::string const&) () from/usr/lib/x86_64-linux-gnu/libstdc++.so.6

#1 0x0000000000400af8 in __static_initialization_and_destruction_0 (__initialize_p=1,

__priority=65535) at Test4.cpp:7

#2 0x0000000000400b24 in _global__sub_i_b () at Test4.cpp:12

#3 0x0000000000400c2d in __libc_csu_init ()

#4 0x00007ff5f02de700 in __libc_start_main () from/lib/x86_64-linux-gnu/libc.so.6

#5 0x00000000004009c9 in _start ()

You can see that when the program statically initializes the global object, the call to the copy constructor of string causes a memory access exception. The cause of this problem is that static members that are dependent are not initialized yet .

We know that all global and static data members for a program are placed in the global data area. For global and static variables that have already been initialized, the data segment (.data) that is stored in the executable, and for uninitialized global and static variables, in the BSS segment (BSS The segment does not exist in the generated executable until the program is loaded into memory, and the program is loaded into memory . BSS the memory of the segment is zeroed out.

Here we first emphasize a concept: static initialization: Initialization of static objects, including global and static variables .

After the program is loaded into memory, the dynamiclinker loader is present before the programmer -specified dynamical initialization occurs for global and static variables in the data segment. will ensure that each static object is initialized to 0 ( of course, this is only for built-in data types ). For a custom data type, such as a string object in a program, if a is not initialized dynamically, then The data for A's memory space is undefined , a copy constructor that invokes string causes a memory access exception to occur. This situation can easily occur across file references.


In thep245 ofC + + programming thought, some suggestions are given for static initialization dependency:

    1. To avoid static object initialization dependency;
    2. Put static objects in the same compilation unit, that is, the same file.
    3. If you must put the static object in a different compilation unit, you can use two programming techniques to solve

Let's just talk about the technology in there. Two: get a static object from a function .

In fact, we always care about the initialization order of the objects, not the initialization time . To solve this problem, this technique uses the following: To put a static object into a function that returns the object reference, the only way to access the static object is through the function, and when the function is called for the first time, the static object is forced to initialize. The technology relies on a static object inside the function to initialize when the function is first called, and only once in the program life cycle . In this way, the initialization order of static objects is determined by the code of the design rather than the link order of the linker. The above code passes the technique and can be changed to the following:

Test1.cpp

#include <string>

using namespace Std;

Const string & Geta ()

{

static string a = "test";

return A;

}

Test2.cpp

#include <string>

#include <iostream>

using namespace Std;

Const string &geta ();

String B = Geta ();

int main ()

{

cout<<b<<endl;

}

The code above does not have the first problem.


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.