静态分配内存在静态取消初始化时会变得无效吗? [英] Can static allocated memory become invalid during static deinitialization?

查看:106
本文介绍了静态分配内存在静态取消初始化时会变得无效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我已经定义了这样的变量(C ++):

Say I have defined a variable like this (C++):

static const char str[] = "Here is some string data";

我有一个静态分配的类实例在析构函数中引用这个数组,例如。

And I have a statically allocated class instance which references this array in its destructor, can this go wrong? E.g. could the str variable somehow get invalid?

class A {
 ~A() {
   cout << str << endl;
 }
};

static A a;

我的假设是它不会出错,但我可以在任何地方找到它。我想知道这一点肯定。我的假设是,我们不能预测在静态分配对象的析构函数被调用的顺序,但数据本身永远不会真正释放,直到过程被拆除。含义指向POD的指针应该是安全的,而不是对象实例。

My assumption is that it can't go wrong, but I can find it clearly stated anywhere. I want to know this for sure. My assumption is that we can not predict the sequence in which destructors for statically allocated objects are called but that the data itself is never really freed until the process is torn down. Meaning pointers to POD should be safe, but not object instances.

含义。 this:

static const QString str = "Here is some string data";

static const std::string str = "Here is some string data";

无法安全地用于 的析构函数,它们在堆上的字符串数据,这可能在调用 的析构函数之前被析构函数释放。

Can not safely be used in A's destructor because they both allocate their string data on the heap and this might be freed by the destructor before A's destructor is called.

有没有C ++标准中的任何部分解释这个或一些链接到一些其他机构谁可以验证这?

Is my assumption right and are there any sections in the C++ standard explaining this or some link to some other authority who can verify this?

推荐答案

阅读C ++标准自己找到一些答案。我从答案中看到,对于构造对象和分配对象之间的区别,有很多困惑。

Okay I tried to read the C++ standard myself to find some answers. I see from the answers I get that there is a lot of confusion about difference between constructing an object and allocating it.

从标准:


3.6.2
非本地对象的初始化

3.6.2 Initialization of non-local objects

静态存储持续时间为$ b的对象在任何其他初始化
发生之前,$ b(3.7.1)应该是零初始化的
(8.5)。具有静态
存储持续时间的引用和具有静态存储持续时间的POD
对象的引用可以用常量
表达式(5.19)初始化
;这就叫做
常量初始化。一起,
零初始化和常量
初始化称为静态
初始化;所有其他
初始化是动态
初始化。静态初始化
必须在任何动态
初始化之前执行。动态
对象的初始化是
有序或无序的。

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. A reference with static storage duration and an object of POD type with static storage duration can be initialized with a constant expression (5.19); this is called constant initialization. Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place. Dynamic initialization of an object is either ordered or unordered.

我的解释是, strong> const char [] 始终保证在运行任何构造函数之前已设置。

My interpretatio of this is that a const char[] will always be guaranteed to have been set before any constructor is run.


3.6.3终止
静态存储持续时间的初始化对象的析构函数(12.4)(在块范围或命名空间范围)是
调用作为从main返回和作为调用std :: exit(18.3)的结果。这些对象在它们的构造函数的完成或它们的动态初始化的完成的
逆序中被销毁。如果一个对象是
被静态初始化,则该对象的销毁顺序与对象被动态初始化的顺序相同。

3.6.3 Termination Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespace scope) are called as a result of returning from main and as a result of calling std::exit (18.3). These objects are destroyed in the reverse order of the completion of their constructor or of the completion of their dynamic initialization. If an object is initialized statically, the object is destroyed in the same order as if the object was dynamically initialized.

从我可以从这个 POD 类型读取的常量表达式将在任何对象类型之前初始化,并在任何对象类型之后被销毁。

From what I can read from this POD types with constant expression will be initialized before any object types and destroyed after any object types. Meaning no code will run that can access them while they are not valid.

这应该解释为什么Google的C ++代码标准说你应该使用具有常量表达式的POD类型

Which should explain why Google's C++ code standard says you should only use POD types with constant expressions.:


因此,我们只允许静态变量包含POD数据。这个规则完全禁止向量(使用C数组)或字符串(使用const char [])。

As a result we only allow static variables to contain POD data. This rule completely disallows vector (use C arrays instead), or string (use const char []).

这篇关于静态分配内存在静态取消初始化时会变得无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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