编译器如何实现静态变量初始化? [英] How is static variable initialization implemented by the compiler?

查看:201
本文介绍了编译器如何实现静态变量初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我声明一个基本类型(char,int,double,...)的静态变量,等等),并给它一个初始值,我想编译器只是在程序的开始处设置该变量的值,然后调用 main()

  void SomeFunction(); 

int main(int argCount,char ** argList)
{
//此时,为'answer'保留的内存
//已包含值42
SomeFunction();
}

void SomeFunction()
{
static int answer = 42;然而,如果静态变量是一个类的实例:


$ b < >

  class MyClass 
{
// ...
};

void SomeFunction();

int main(int argCount,char ** argList)
{
SomeFunction();
}

void SomeFunction()
{
static MyClass myVar;
}



我知道它不会被初始化,直到第一次函数称为。因为编译器没有办法知道该函数何时第一次被调用,它如何产生这种行为?是否在函数体中引入了一个if块?

  static bool initialized = 0; 
if(!initialized)
{
//构造myVar
initialized = 1;
}


解决方案

函数局部静态变量的初始化完全与你想象的一样。



注意,一般来说,这不是以线程安全的方式完成的。所以如果你有静态局部变量的函数,可以从多个线程调用,你应该考虑到这一点。在调用任何其他函数之前,在主线程中调用该函数通常会做到这一点。



我应该补充一点,如果本地静态的初始化是一个简单的常量像在你的例子中,编译器不需要经过这些回转 - 它可以只是初始化图像中的变量或 main()像一个常规的静态初始化因为你的程序不能告诉区别)。但是如果你用一个函数的返回值来初始化它,那么编译器就必须测试一个标志,指示初始化是否已经完成或者是等价的。


I'm curious about the underlying implementation of static variables within a function.

If I declare a static variable of a fundamental type (char, int, double, etc.), and give it an initial value, I imagine that the compiler simply sets the value of that variable at the very beginning of the program before main() is called:

void SomeFunction();

int main(int argCount, char ** argList)
{
    // at this point, the memory reserved for 'answer'
    // already contains the value of 42
    SomeFunction();
}

void SomeFunction()
{
    static int answer = 42;
}

However, if the static variable is an instance of a class:

class MyClass
{
    //...
};

void SomeFunction();

int main(int argCount, char ** argList)
{
    SomeFunction();
}

void SomeFunction()
{
    static MyClass myVar;
}

I know that it will not be initialized until the first time that the function is called. Since the compiler has no way of knowing when the function will be called for the first time, how does it produce this behavior? Does it essentially introduce an if-block into the function body?

static bool initialized = 0;
if (!initialized)
{
    // construct myVar
    initialized = 1;
}

解决方案

In the compiler output I have seen, function local static variables are initialized exactly as you imagine.

Note that in general this is not done in a thread-safe manner. So if you have functions with static locals like that that might be called from multiple threads, you should take this into account. Calling the function once in the main thread before any others are called will usually do the trick.

I should add that if the initialization of the local static is by a simple constant like in your example, the compiler doesn't need to go through these gyrations - it can just initialize the variable in the image or before main() like a regular static initialization (because your program wouldn't be able to tell the difference). But if you initialize it with a function's return value, then the compiler pretty much has to test a flag indicating if the initialization has been done or something equivalent.

这篇关于编译器如何实现静态变量初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆