C ++静态初始化vs __attribute __((constructor)) [英] C++ static initialization vs __attribute__((constructor))

查看:1605
本文介绍了C ++静态初始化vs __attribute __((constructor))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

struct Foo { Foo() { printf("foo\n"); } };
static Foo foo;

__attribute__((constructor)) static void _bar() { printf("bar\n"); }

是确定性的还是 foo code> bar 首先打印?

Is it deterministic wether foo or bar is printed first?

(我希望并且希望静态对象的构造函数总是首先执行, )

(I hope and would expect that constructors of static objects are always executed first but not sure and GCCs doc about the constructor attribute doesn't say anything about it.)

推荐答案

foo

foo will be printed first, as the objects are initialized in the order of their declarations. Run and see:

  • Ideone online demo

顺便说一下, __ attribute __((constructor))不是标准C ++。它是GCC的扩展。所以你的程序的行为取决于GCC如何定义它。总之,它是实现定义的,根据它 foo 被首先打印。

By the way, __attribute__((constructor)) is not Standard C++. It is GCC's extension. So the behavior of your program depends on how GCC has defined it. In short, it is implementation-defined, according to it foo is printed first.

doc 说,


constructor属性使得函数在执行进入main()之前被自动调用。类似地,destructor属性使得函数在main()完成或exit()被调用后自动调用。具有这些属性的函数可用于初始化在程序执行期间隐式使用的数据。

The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () has completed or exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.

您可以提供一个可选的整数优先级来控制构造函数并运行析构函数。具有较小优先级编号的构造函数在具有较大优先级编号的构造函数之前运行;相反的关系适用于析构函数。因此,如果您有一个分配资源的构造函数和释放相同资源的析构函数,则这两个函数通常具有相同的优先级。 构造函数和析构函数的优先级与为命名空间范围的C ++对象指定的优先级相同(参见C ++属性)。

You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see C++ Attributes).

我认为文本以粗体表示,对象按照它们的声明的顺序初始化,正如我之前说的,这是由在线演示

I think the text in bold implies, the objects are initialized in the order of their declarations, as I said before, which is pretty much confirmed by online demo also.

我想你也想读这个:

  • 7.7 C++-Specific Variable, Function, and Type Attributes

如果要控制/更改初始化顺序,可以使用 init_priority 属性,提供优先级。摘自网页

If you want to control/alter the initialization order, you can use init_priority attribute, providing priority. Taken from the page:

Some_Class  A  __attribute__ ((init_priority (2000)));
Some_Class  B  __attribute__ ((init_priority (543)));

这里, B c $ c> A

Here, B is initialized before A.

这篇关于C ++静态初始化vs __attribute __((constructor))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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