Home > Backend Development > C++ > body text

Why Can Global Variables in Different Translation Units Lead to Unexpected Behavior in C ?

Barbara Streisand
Release: 2024-10-30 02:51:28
Original
946 people have browsed it

 Why Can Global Variables in Different Translation Units Lead to Unexpected Behavior in C  ?

Global Initialization Order and Dependency Resolution in C

In C , global variables within a single translation unit are initialized in the order they are defined. However, there is no well-defined order for global variables across different translation units. This ambiguity can lead to unexpected behavior, as illustrated in the following code:

<code class="cpp">struct Foo {
    Foo() { printf("Foo::Foo()\n"); }
    void add() { printf("Foo::add()\n"); }
    static int addToGlobal() {
        printf("Foo::addToGlobal() START\n");
        globalFoo.add();
        printf("Foo::addToGlobal() END\n");
        return 0;
    }
};

Foo globalFoo;
int dummy = Foo::addToGlobal();</code>
Copy after login

When addToGlobal is called before globalFoo is initialized, the behavior is as expected:

Foo::Foo()
Foo::addToGlobal() START
Foo::add()
Foo::addToGlobal() END
main()
Copy after login

However, when the order is reversed, the constructor for Foo is not called, and globalFoo is somehow accessible within addToGlobal:

Foo::addToGlobal() START
Foo::add()
Foo::addToGlobal() END
Foo::Foo()
main()
Copy after login

This behavior is attributed to the fact that the order of initialization for globals ignores any dependencies between them. In this case, dummy depends on globalFoo, but their initialization order is not guaranteed.

To ensure that the constructor for Foo is called before using globalFoo, one solution is to create a static pointer to the global instance and test whether it is null within addToGlobal. If it is null, the global Foo is created before any dynamic initialization.

The above is the detailed content of Why Can Global Variables in Different Translation Units Lead to Unexpected Behavior in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!