数组定义保证 [英] Array definition guarantees

查看:59
本文介绍了数组定义保证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以问。


如果定义了一个数组,而不是一个静态的,但在函数之外,

是否有任何保证每个元素的内容?


谢谢。

May I ask.

If an array is defined , not as a static, but outside of a function,
is there any guarantee as to the contents of each element?

Thanks.

推荐答案

8月12日,9:36 * pm ,mdh< md ** @ comcast.netwrote:
On Aug 12, 9:36*pm, mdh <md**@comcast.netwrote:

请问。


如果定义了一个数组,不是静态的,而是在函数之外,

是否对每个元素的内容有任何保证?
May I ask.

If an array is defined , not as a static, but outside of a function,
is there any guarantee as to the contents of each element?



变量(在

函数之外声明的任何类型(包括数组)总是具有静态存储持续时间,并使用''static''存储类声明这样的
变量使其成为可能有内部

的联系。所以是的,保证在函数外部声明的数组

将其所有元素设置为零。


Sebastian

A variable (of any kind, including an array) declared outside a
function always has static storage duration, and declaring such a
variable with the ''static'' storage class makes it have internal
linkage. So yes, an array declared outside a function is guaranteed
have all its elements set to zero.

Sebastian


8月12日,7:51 * pm,s0s ... @ gmail.com写道:
On Aug 12, 7:51*pm, s0s...@gmail.com wrote:

8月12日,9 :36 * pm,mdh< m ... @ comcast.netwrote:
On Aug 12, 9:36*pm, mdh <m...@comcast.netwrote:


如果定义了一个数组,不是作为静态,而是在函数之外,

是否对每个元素的内容有任何保证?
If an array is defined , not as a static, but outside of a function,
is there any guarantee as to the contents of each element?





函数之外声明的变量(任何类型,包括数组)总是有静态存储持续时间,并声明这样的<带有''静态''存储类的
变量使得它具有内部的

链接。所以是的,保证在函数外部声明的数组

将其所有元素设置为零。


A variable (of any kind, including an array) declared outside a
function always has static storage duration, and declaring such a
variable with the ''static'' storage class makes it have internal
linkage. So yes, an array declared outside a function is guaranteed
have all its elements set to zero.



所以,为了确保,在声明/定义中使用实际单词

''static''的唯一区别是该变量的范围?


So,just to be sure, the only difference in using the actual word
''static'' in the declaration/definition is the scope of that variable?


On Tue ,2008年8月12日19:36:02 -0700(PDT),mdh< md ** @ comcast.netwrote
comp.lang.c中的

On Tue, 12 Aug 2008 19:36:02 -0700 (PDT), mdh <md**@comcast.netwrote
in comp.lang.c:

我可以问。


如果定义了一个数组,不是静态的,而是在函数之外,

有没有保证每个元素的内容?


谢谢。
May I ask.

If an array is defined , not as a static, but outside of a function,
is there any guarantee as to the contents of each element?

Thanks.



您对C中的关键字

static长期存在的两种不同含义感到有些困惑。 1999年对标准的更新

增加了第三个与此无关的。


在文件范围内定义的所有数据对象,这意味着在任何

函数,具有静态存储持续时间和外部链接。如果你

添加静态关键字到这样的定义,你改变了从外部到内部的联系

,这意味着该对象不能被另一个翻译单元的名称引用

,大致是其他来源

文件。


函数内定义的任何数据对象都有自动存储

持续时间没有链接。除非定义包含初始化程序,否则该对象不会初始化为

all中的任何值,并且当函数返回时

消失,并且不会通过一次调用保留其值

下一个功能。如果你添加静态关键字到这样一个

定义,它将存储持续时间从自动更改为静态,

所以对象持续了程序的生命并保持最后一个

从一个函数调用到下一个函数调用的值。


所以为了回答你的问题,所有具有静态存储持续时间的对象都是
并且没有明确的初始化程序在定义中是零初始化。

所有整数类型初始化为0,浮点类型初始化为0.0,

和指针类型为NULL。


这适用于:


- 文件范围内没有静态的对象关键字


- 文件范围内的对象,带有静态对象关键字


- 函数和块范围内的对象,带有static和static。关键字


因此,在函数外部的文件范围定义的数组是

保证为零初始化。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

You are a bit confused by the two different meanings that the keyword
static has had in C for a long time. The 1999 update to the standard
added a third one that is not related here.

All data objects defined at file scope, which means outside of any
function, has static storage duration and external linkage. If you
add the "static" keyword to such a definition, you change the linkage
from external to internal, which means the object cannot be referenced
by that name from another translation unit, roughly other source
files.

Any data objects defined inside a function have automatic storage
duration no linkage. The object is not initialized to any value at
all unless the definition includes an initializer, and disappears when
the function returns, and does not retain its value from one call of
the function to the next. If you add the "static" keyword to such a
definition, it changes the storage duration from automatic to static,
so the object lasts for the life of the program and keeps the last
assigned value from one function call to the next.

So to answer your question, all objects with static storage duration
and no explicit initializer in the definition are zero-initialized.
All integer types are initialized to 0, floating point types to 0.0,
and pointer types to NULL.

This applies to:

-- objects at file scope without the "static" keyword

-- objects at file scope with the "static" keyword

-- objects at function and block scope with the "static" keyword

So your array defined at file scope, outside of a function, is
guaranteed to be zero-initialized.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


这篇关于数组定义保证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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