English 中文(简体)
静态分配的记忆在静态的初始阶段是否无效?
原标题:Can static allocated memory become invalid during static deinitialization?

我已经对这一变化(C++):

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

我有一个静态分配的班级,在座右翼提到这一阵列,这是否是错误的? 比如,str 可变的某种how是否无效?

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

static A a;

我的假设是,它可能不会错,但我可以发现,它在任何地方都明确表示了这一点。 我很想知道这一点。 我的假设是,我们无法预测静态分配物体的离轨器的顺序,但数据本身从来就没有真正自由,直到这一进程被打断为止。 裁军厅的Meaning Pointers应当是安全的,但不反对。

例如:

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 destruct或 because they both allocate their string data on the heap and this might be freed by the destruct或 bef或e A s destruct或 is called.

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

最佳回答

Okay I试图读C++标准,以找到一些答案。 我从答复中看到,我发现,在构造物体和分配物体之间有很大的混淆。

根据标准:

3.6.2 Initialization of non-local objects

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.

我的解释是,在任何建筑商经营之前,将始终保证建造一个const char[]

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型,随时表达的,将先入选,然后在任何物体类型之后销毁。 没有任何法典可以进入,但无效。

我们应当解释,为什么谷歌的C++代码标准只要求你使用

因此,我们只允许静态变量包含多氯二苯并对二恶英的数据。 本条规则完全排除了病媒(改用C阵列),或扼杀(使用 con[])。

问题回答

所谓自动、全球和静态物体的离轨器的顺序总是明确界定的:它推翻了要求施工者的顺序。 因此,如果标的<代码>A参引物体BB>在前构造<代码>A/code>,则可以确定。 www.un.org/spanish/ga/president 这使我们想到建筑商的次序问题。

我记得:

  1. Global and static objects are constructed before main() is called.
  2. Class-local statics are constructed before any object of their class.
  3. Function-local statics are constructed when their scope is reached for the first time.
  4. Global and static objects within the same translation unit are constructed in the order they are defined. (That means that the order of inclusion of headers might affect this!)
  5. The order of construction of global and static objects across translation units is undefined.

如果你认为这是正确的,请更正我。

如果我正确记住,标准(或定义不明确)没有界定全球物体的初始化,使全球物体难以相互参照。

如果你想确定初始化令,使用全球功能,并用你在其中的静态目标,那就回去了。 您现在再次保证,静态物体将按第一功能要求开始。

销毁工作将在申请结束时进行,一度在主(主)之外。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?