静态和初始化规则或0是0.0是NULL [英] static and initialization rules or 0 is 0.0 is NULL

查看:69
本文介绍了静态和初始化规则或0是0.0是NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道怎么想以下......

(来自dietlibc FAQ)

问:我看到很多未初始化的变量,比如 ; static int foo;"。什么

给出了什么?

A:静态全局变量初始化为0. ANSI C保证。

从技术上讲,静态变量进入.bss ELF段,

而静态int foo = 0"进入.data。因为OS填充的.bss为零,所以它不需要在实际的二进制文件中。所以,如果所需的

初始化值为0,那么
实际上最好不要初始化静态变量。对于指针也是如此,通过

的方式。在饮食libc支持的所有平台上,数字零

也是NULL的指针值。因此,不初始化静态
指针会产生NULL。


到目前为止,我仍在手动初始化所​​有变量。我可以节省很多

的代码如果我删除所有静态的零/ NULL初始化

全局...

我是一种但是担心会出现轻微的破损。一个平台0

!= NULL是否真的存在? ANSI标准是否允许这样的

平台?

这个初始化为0的距离是多少?保证去?浮点

值是否保证为0.0? char数组的所有字节都是0x00吗?怎么样呢?
结构?


我从来没有大胆过去只做memset(结构,0,

sizeof(结构) ))如果我希望结构的所有成员都是某种类型的零(NULL指针,0浮点数,0字节,0 int)。


应该怎么办?

解决方案

2008年6月2日星期一00:58:50 + 0200,copx < co ** @ gazeta.plwrote:


>我不知道怎么想以下......
(来自dietlibc)常见问题)
问:我看到很多未初始化的变量,例如static int foo;。
给出了什么?
A:静态全局变量初始化为0. ANSI C保证。



第一句由标准保证。


从技术上讲,静态变量进入。 bss ELF细分,



其余部分特定于此特定系统,与您的

问题无关。


而static int foo = 0进入.data。因为OS填充的.bss为零,所以它不需要在实际的二进制文件中。所以,如果所需的

初始化值为0,那么
实际上最好不要初始化静态变量。对于指针也是如此,通过

的方式。在饮食libc支持的所有平台上,数字零

也是NULL的指针值。因此,不初始化静态的
指针会产生NULL。

到目前为止,我仍在手动初始化所​​有变量。如果我删除静态
全局变量的所有零/ NULL初始化,我可以节省很多代码..



虽然你可以保存很多打字,它不太可能保存

任何代码执行,除非在一个系统上(反过来)使得它是一个

点来处理默认初始化不同于

初始化为默认值。


>不过,我有点担心轻微的破损。实际存在0
!= NULL的平台是否真实存在? ANSI标准是否允许这样的平台?



您将值0与将对象设置为所有位零混淆。

虽然这适用于各种整数类型,但它是不保证

适用于其他类型。


当int literal 0被分配给一个对象指针时,该指针

将为该类型的指针分配NULL值。在其他

字中

ptr = NULL;



ptr = 0;

保证具有相同的效果。类似的保证是

用于比较(ptr == NULL和ptr == 0和!=)。它是如此

远远包括条件语句,如if和while以及

三元运算符?:( if(ptr)将评估相同的if(ptr!=

0))。


这些都没有告诉你关于

指针的位表示的任何内容NULL值。是的,标准

确实允许NULL指针具有除所有位之外的表示

零。但是,在这种情况下,编译器必须生成正确的

代码,以便ptr = 0;仍然会导致分配给指针的正确NULL值为




顺便说一句,浮点数也是如此。 0.0不一定是所有位0代表的


>这个初始化为0的距离是多少?保证去?是浮点



一路。


>值保证为0.0? char数组的所有字节都是0x00吗?
结构怎么样?



是的。只有当CHAR_BIT为8时(否则在
x之后你需要更多的零)。一个结构(和任何其他聚合)用

初始化相同的规则递归地应用于结构的成员

(聚合的元素)。
< blockquote class =post_quotes>
>
如果我想要结构的所有成员,我从来没有大胆过去只做memset(结构,0,
sizeof(结构))是某种零(NULL指针,0浮点数,0字节,0 int)。



虽然它可以在您的系统上运行,但它不可移植。


>



让编译器为你工作。

删除电子邮件的del




" copx" < co ** @ gazeta.plwrote in message

news:g1 ********** @ inews.gazeta.pl ...


>我不知道怎么想以下......

(来自dietlibc FAQ)

问:我看到很多未初始化的变量,例如static int foo;。什么

给出了什么?

A:静态全局变量初始化为0. ANSI C保证

即。

从技术上讲,静态变量进入.bss ELF段,

而static int foo = 0进入.data。因为OS填充的.bss为零,所以它不需要在实际的二进制文件中。所以,如果所需的

初始化值为0,那么
实际上最好不要初始化静态变量。对于指针也是如此,通过

的方式。在饮食libc支持的所有平台上,数字零

也是NULL的指针值。因此,不初始化静态
指针会产生NULL。


到目前为止,我仍在手动初始化所​​有变量。如果我删除静态

全局的所有零/ NULL初始化,我可以保存很多代码。



您可以保存一些打字。我怀疑它会使程序变小。


-

Bartc


Barry Schwarz< ; sc ****** @ dqel.comwrites:


On Mon,2008年6月2日00:58:50 +0200," copx" < co ** @ gazeta.plwrote:



< snip your excellent explainations>


>>值保证为0.0? char数组的所有字节都是0x00吗?
结构怎么样?



是的。只有当CHAR_BIT为8时(否则在
x之后你需要更多的零)。



当然,0x00只是写0的另一种方式,并且零会初始化任意宽度的

字符。

< snip>


>>我从未大胆过去只做memset(结构,0,
sizeof(结构) ))如果我希望结构的所有成员都是零(NULL指针,0浮点数,0字节,0 int)。



虽然它可以在您的系统上运行,但它不可移植。


>>应该怎么做?



到OP:一个便携式替代方案是写一个归零功能

像这样:


void zero_some_struct(struct S * sp)

{

static struct S = {0};

* sp = S;

}


并不总是一个好主意,但值得考虑。


-

Ben。


I don''t know what to think of the following..
(from the dietlibc FAQ)
Q: I see lots of uninitialized variables, like "static int foo;". What
gives?
A: "static" global variables are initialized to 0. ANSI C guarantees that.
Technically speaking, static variables go into the .bss ELF segment,
while "static int foo=0" goes into .data. Because .bss is zero
filled by the OS, it does not need to be in the actual binary. So it
is in fact better to not initialize static variables if the desired
initialization value is 0 anyway. The same is true for pointers, by
the way. On all platforms supported by the diet libc, numeric zero
is also the pointer value for NULL. So not initializing a static
pointer yields NULL.

So far I am still initializing all my variables by hand. I could save a lot
of code if I removed all the to zero/NULL initializations for static
globals..
I am kind of worried about subtile breakage, though. Does a platform where 0
!= NULL is true actually exist? Does the ANSI standard even allow such
platforms?
And how far does this "intialized to 0" guarantee go? Are floating point
values guaranteed to be 0.0? Are all bytes of char array 0x00? What about
structures?

I have never been bold enough to just do memset(structure, 0,
sizeof(structure)) if I wanted all members of the structure to be some kind
of zero (NULL pointer, 0 float, 0 byte, 0 int).

What should one do?

解决方案

On Mon, 2 Jun 2008 00:58:50 +0200, "copx" <co**@gazeta.plwrote:

>I don''t know what to think of the following..
(from the dietlibc FAQ)
Q: I see lots of uninitialized variables, like "static int foo;". What
gives?
A: "static" global variables are initialized to 0. ANSI C guarantees that.

The first sentence is guaranteed by the standard.

Technically speaking, static variables go into the .bss ELF segment,

The rest is specific to this particular system and irrelevant to your
question.

while "static int foo=0" goes into .data. Because .bss is zero
filled by the OS, it does not need to be in the actual binary. So it
is in fact better to not initialize static variables if the desired
initialization value is 0 anyway. The same is true for pointers, by
the way. On all platforms supported by the diet libc, numeric zero
is also the pointer value for NULL. So not initializing a static
pointer yields NULL.

So far I am still initializing all my variables by hand. I could save a lot
of code if I removed all the to zero/NULL initializations for static
globals..

While you may save a lot of typing, it is unlikely that it will save
any code execution except on a system which (perversely) makes it a
point to treat the default initialization different than
initialization to the default value.

>I am kind of worried about subtile breakage, though. Does a platform where 0
!= NULL is true actually exist? Does the ANSI standard even allow such
platforms?

You are confusing the value 0 with setting an object to all bits zero.
While this works for the various integer types, it is not guaranteed
to work for other types.

When the int literal 0 is assigned to an object pointer, that pointer
will be assigned the NULL value for that type of pointer. In other
words
ptr = NULL;
and
ptr = 0;
are guaranteed to have the same effect. A similar guarantee is
provided for comparison (ptr == NULL and ptr == 0 and !=). It goes so
far as to include conditional statements like if and while and the
ternary operator ?: (if (ptr) will evaluate the same as if (ptr !=
0)).

None of this tells you anything about the bit representation of a
pointer which has been assigned the NULL value. Yes, the standard
does allow a NULL pointer to have a representation other than all bits
zero. However, in this case the compiler must generate the correct
code so that ptr = 0; still results in the correct NULL value being
assigned to the pointer.

By the way, the same is true for floating point. 0.0 need not be
represented by all bits 0.

>And how far does this "intialized to 0" guarantee go? Are floating point

All the way.

>values guaranteed to be 0.0? Are all bytes of char array 0x00? What about
structures?

Yes. Only when CHAR_BIT is 8 (otherwise you need more zeros after the
x). A structure (and any other aggregate) are initialized with the
same rules applied recursively to the members of the structure
(elements of the aggregate).

>
I have never been bold enough to just do memset(structure, 0,
sizeof(structure)) if I wanted all members of the structure to be some kind
of zero (NULL pointer, 0 float, 0 byte, 0 int).

While it will work on your system, it would not be portable.

>
What should one do?

Let the compiler do the work for you.
Remove del for email



"copx" <co**@gazeta.plwrote in message
news:g1**********@inews.gazeta.pl...

>I don''t know what to think of the following..
(from the dietlibc FAQ)
Q: I see lots of uninitialized variables, like "static int foo;". What
gives?
A: "static" global variables are initialized to 0. ANSI C guarantees
that.
Technically speaking, static variables go into the .bss ELF segment,
while "static int foo=0" goes into .data. Because .bss is zero
filled by the OS, it does not need to be in the actual binary. So it
is in fact better to not initialize static variables if the desired
initialization value is 0 anyway. The same is true for pointers, by
the way. On all platforms supported by the diet libc, numeric zero
is also the pointer value for NULL. So not initializing a static
pointer yields NULL.

So far I am still initializing all my variables by hand. I could save a
lot of code if I removed all the to zero/NULL initializations for static
globals..

You might save some typing. I doubt it will make the program smaller.

--
Bartc


Barry Schwarz <sc******@dqel.comwrites:

On Mon, 2 Jun 2008 00:58:50 +0200, "copx" <co**@gazeta.plwrote:

<snip your excellent explanations>

>>values guaranteed to be 0.0? Are all bytes of char array 0x00? What about
structures?


Yes. Only when CHAR_BIT is 8 (otherwise you need more zeros after the
x).

Surely 0x00 is just another way to write 0 and will zero initialise a
char of any width.

<snip>

>>I have never been bold enough to just do memset(structure, 0,
sizeof(structure)) if I wanted all members of the structure to be some kind
of zero (NULL pointer, 0 float, 0 byte, 0 int).


While it will work on your system, it would not be portable.

>>What should one do?

To the OP: one portable alternative is to write a zeroing function
like this:

void zero_some_struct(struct S *sp)
{
static struct S = {0};
*sp = S;
}

not always a good idea, but worth considering.

--
Ben.


这篇关于静态和初始化规则或0是0.0是NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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