零初始化。 [英] zero initialization.

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

问题描述

大家好,


我最近偶然发现了这段代码。根据什么

我在标准中看到它应该没问题,但是我一直都做了初步明确的初始化:


//.h

// ...

struct X

{

静态Y * m_Y;

};


//.cpp

//初始化......

Y * X :: m_Y; //...1

//而不是......

Y * X :: m_Y = NULL; //...2


我现在知道(1)也是正确的(来自

标准的各个区域)。

我个人更喜欢(2) - 可能是因为我一直都这样做

方式(我想我甚至可能有问题(1)一对夫妇

年前。)


对此有何想法?我是否正确理解标准如果我说b $ b说(1)没问题?


我从std98获得的理由:

- 8.5 :5 - 零初始化...

- 3.6.2 - 存储具有静态存储的对象...


您认为哪种方法更受欢迎,或者是品味问题?


问候,


Werner

Hi all,

I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I''ve always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;
};

//.cpp
//To initialise...
Y* X::m_Y; //...1
//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the
standard).
I personally prefer (2) - probably because I''ve always done it that
way (I think I might have even had a problem with (1) a couple
of years back).

Any thoughts on this? Do I understand the standard correctly if I
say (1) is OK?

My rationale from std98:
- 8.5:5 - to zero initialize...
- 3.6.2 - storage for objects with static storage...

Which method do you consider preferred, or is it a matter of taste?

Regards,

Werner

推荐答案

12月19日晚上10点16分,werasm< wer ... @ gmail.comwrote:
On Dec 19, 10:16 pm, werasm <wer...@gmail.comwrote:

大家好,


我最近偶然发现了这段代码。根据什么

我在标准中看到它应该没问题,但是我一直都做了初步明确的初始化:


//.h

// ...

struct X

{

静态Y * m_Y;


};


//.cpp

//初始化...

Y * X :: m_Y; //...1
Hi all,

I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I''ve always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;

};

//.cpp
//To initialise...
Y* X::m_Y; //...1



我认为存储在数据段bss中的静态变量总是

初始化为0 ...

就像全局变量一样...

I think static variables stored in the data segment bss, are always
initialized with 0...
just like global variables...


//而不是......

Y * X: :m_Y = NULL; //...2


我现在知道(1)也是正确的(来自
中的各个区域
//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the


Rahul写道:
Rahul wrote:

12月19日晚上10点16分,werasm< wer ... @ gmail.comwrote:
On Dec 19, 10:16 pm, werasm <wer...@gmail.comwrote:

>大家好,

我最近偶然发现了这个代码。根据我在标准中看到的内容,应该没问题,但是我我总是自己完成初始化
//.h
// ...
struct X
{
static Y * m_Y;

};

//.cpp
//初始化......
Y * X :: m_Y; // ... 1
>Hi all,

I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I''ve always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;

};

//.cpp
//To initialise...
Y* X::m_Y; //...1



我认为存储在数据段bss中的静态变量总是

初始化为0 ...


I think static variables stored in the data segment bss, are always
initialized with 0...



澄清...


未指明它们存储在哪个段中,并且没有

引用C ++语言标准中的bss。对象wi静态

存储持续时间在任何其他动态初始化之前将其内存零初始化。如果没有其他初始化是预期的,那么零填充内存就是它。

To clarify...

It is unspecified what "segment" they are stored in, and there is no
reference to "bss" in the C++ language standard. Objects with static
storage duration have their memory zero-initialised before any other
dynamic initialisation takes place. If no other initialisation is
expected, the zero-filled memory is it.


就像全局变量一样...
just like global variables...



全局变量(在命名空间级别声明)都具有静态

存储持续时间(如果它们有存储空间,那就是)。

Global variables (declared at the namespace level) all have static
storage duration (if they have storage at all, that is).


> //而不是......
Y * X :: m_Y = NULL; //...2

我现在知道(1)也是正确的(来自
的各个区域
>//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the



V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


12月19日上午9:16,werasm< wer ... @ gmail.comwrote:
On Dec 19, 9:16 am, werasm <wer...@gmail.comwrote:

>

您认为哪种方法更受欢迎,或者是品味问题?
>
Which method do you consider preferred, or is it a matter of taste?



我认为(2)是首选。一个''静态成员''意味着所有

对象

的X共享同一个成员。''静态''这里不需要

''my_Y'在静态存储中,尽管在许多

实现中确实如此。


JG

I think that (2) is preferred. A ''static member'' means that all
objects
of X shares the same member. The ''static'' here does not require
that ''my_Y'' be in static storage, although it is in fact true in many
implementations.

JG


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

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