关于初始化非本地对象 [英] About initializing non-local object

查看:63
本文介绍了关于初始化非本地对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。当我查阅ISO C ++标准时,我注意到在

第3.6.2.1段中,标准规定:

具有静态存储持续时间的对象应在之前进行零初始化

任何其他初始化都会发生。


这是否意味着所有非本地对象将在

之前进行零初始化它们是由初始化器初始化的(如果有的话)?例如:


int g_var = 3;

int main(){}


将g_var作为第一个初始化为零,然后是3?

Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other initialization takes place."

Does this mean all non-local objects will be zero-initialized before
they are initialized by their initializers(if they have)? For example:

int g_var = 3;
int main() {}

Will g_var be first initialized to zero, then to 3?

推荐答案

5月14日,8:20 * pm,WaterWalk< toolmas ... @ 163.comwrote :
On May 14, 8:20*pm, WaterWalk <toolmas...@163.comwrote:

您好。当我查阅ISO C ++ *标准时,我注意到在

第3.6.2.1段中,标准规定:

具有静态存储持续时间的对象应为零初始化在

之前发生任何其他初始化。


这是否意味着所有非本地对象将在
$ b $之前进行零初始化b它们是由初始化器初始化的(如果有的话)?例如:


int g_var = 3;

int main(){}


将g_var作为第一个初始化为零,然后是3?
Hello. When I consult the ISO C++ *standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other initialization takes place."

Does this mean all non-local objects will be zero-initialized before
they are initialized by their initializers(if they have)? For example:

int g_var = 3;
int main() {}

Will g_var be first initialized to zero, then to 3?



是的。或者更确切地说,如果一个程序要观察g_var'的值

这是g_var'值的初始化序列 - 那个

程序会看到。


Greg

Yes. Or more accurately, if a program were to observe g_var''s value
that is the initialization sequence of g_var''s value - that that
program would see.

Greg


WaterWalk写道:
WaterWalk wrote:

你好。当我查阅ISO C ++标准时,我注意到在

第3.6.2.1段中,标准规定:

具有静态存储持续时间的对象应在之前进行零初始化

发生任何其他初始化。
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other initialization takes place."



那是草稿吗?我说:


具有静态存储持续时间的对象的存储应该是

在进行任何其他初始化之前进行零初始化。


这听起来更合乎逻辑,因为在C ++中,一个对象可以定义为
仅初始化一次。

Is that a draft? Mine says:

"The storage for objects with static storage duration shall be
zero-initialized before any other initialization takes place."

This sounds more logical, since in C++, an object can by definition be
initialized only once.


这是否意味着所有非本地对象都将在初始化之前进行零初始化

它们是由初始化器初始化的(如果有的话)?
Does this mean all non-local objects will be zero-initialized before
they are initialized by their initializers(if they have)?



我猜它确实 - 或多或少。见下文。

I guess it does - more or less. See below.


例如:


int g_var = 3;

int main( ){}


首先将g_var初始化为零,然后再初始化为3?
For example:

int g_var = 3;
int main() {}

Will g_var be first initialized to zero, then to 3?



嗯,你没有注意到任何差异,因为它会被初始化为3

,然后才能进入第一个时间。在这种情况下,可以使用as-if

规则,即该程序并不需要完全按照标准所说的那样执行
。它必须具有与

程序相同的可观察效果。由于没有符合标准的方法来观察

对象的存储在对象的真实初始化之前被初始化为零,编译器可以自由地不这样做。

这与动态初始化的对象更相关(即

类与构造函数)。

Well, you won''t notice any difference, since it will be initialized to 3
before you can access it for the first time. In such a case, the "as-if"
rule can be used, that says that the program doesn''t need to do exactly as
the standard says. It just has to have the same observable effect as a
program that would. Since there is no standard-compliant way to observe
that the object''s storage is initialized to zero before the object''s real
initialization, compilers are free to not do that.
This is more relevant for objects that get initialized dynamically (i.e.
classes with constructors).


5月15日下午12:19,Rolf Magnus< ramag ... @ t-online.dewrote:
On May 15, 12:19 pm, Rolf Magnus <ramag...@t-online.dewrote:

WaterWalk写道:
WaterWalk wrote:

你好。当我查阅ISO C ++标准时,我注意到在

第3.6.2.1段中,标准规定:

具有静态存储持续时间的对象应在之前进行零初始化

发生任何其他初始化。
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other initialization takes place."



那是草稿吗?我说:


具有静态存储持续时间的对象的存储应该是

在进行任何其他初始化之前进行零初始化。


这听起来更合乎逻辑,因为在C ++中,一个对象可以定义为

仅初始化一次


Is that a draft? Mine says:

"The storage for objects with static storage duration shall be
zero-initialized before any other initialization takes place."

This sounds more logical, since in C++, an object can by definition be
initialized only once



Mine是2003版。也许你的是1998版。

Mine is the 2003 version. Maybe yours is the 1998 version.


这篇关于关于初始化非本地对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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