重新初始化静态数组??? [英] Reinitializing static array???

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

问题描述

假设我有一个静态的结构数组,其元素可以是任何可以想象的C类型,指针,数组的混合物。

这个数组是未初始化的程序启动。


如果程序后期我想将这个数组返回到它的
启动状态,可以通过写二进制来完成吗

使用memset()将整个内存块归零。例如,


静态结构mystruct_st {

int x1;

int * x2;

double x3 [10];

- - -

} myarray [20];


/ *用myarray做事* /

- - -

/ *恢复初始myarray * /

memset(我的,0,sizeof(myarray));


(其中 - - - 表示其他有效代码)


或者元素的初始值取决于

版本或C的实现?

感谢您的帮助。


问候,

Charles Sullivan


Assume I have a static array of structures the elements of which
could be any conceivable mixture of C types, pointers, arrays.
And this array is uninitialized at program startup.

If later in the program I wish to return this array to its
startup state, can this be accomplished by writing binary
zeroes to the entire memory block with memset(). E.g.,

static struct mystruct_st {
int x1;
int *x2;
double x3[10];
- - -
} myarray[20];

/* Do things with myarray */
- - -
/* Restore initial myarray */
memset(my, 0, sizeof(myarray));

(where "- - -" indicates other valid code)

Or do the initial values of the elements depend on the
version or implementation of C?

Thanks for your help.

Regards,
Charles Sullivan



推荐答案

Charles Sullivan< cw ****** @ triad.rr.com> ;写道:
Charles Sullivan <cw******@triad.rr.com> wrote:
static struct mystruct_st {
int x1;
int * x2;
double x3 [10];
- - -
} myarray [20];
/ *用myarray做事* /
- - -
/ *恢复初始myarray * /
memset(my,0,sizeof(myarray));
或者元素的初始值是否取决于C的版本或实现?
static struct mystruct_st {
int x1;
int *x2;
double x3[10];
- - -
} myarray[20]; /* Do things with myarray */
- - -
/* Restore initial myarray */
memset(my, 0, sizeof(myarray)); Or do the initial values of the elements depend on the
version or implementation of C?




是的。具体来说,对于int *,all-bits-zero不保证是有效的

值; NULL的值是依赖于实现的,

这可能是你想要x2初始化的。


-

Christopher Benson -Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Yes. Specifically, all-bits-zero is not guaranteed to be a valid
value for an int *; the value of NULL is implementation dependent,
which is presumably what you want x2 initialized to.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Charles Sullivan写道:
Charles Sullivan wrote:
假设我有一个静态的结构数组,其元素可以是任何可以想象的C类型,指针,数组的混合。
这个数组在程序启动时未初始化。

如果在程序的后期,我希望将此数组返回到其启动状态,这可以通过使用memset()将二进制零写入整个内存块来实现。

< snip>

不,你不能,至少不便携。特别是,全零的

位模式不能保证将浮点变量初始化为
0.0,它不能保证是空指针值(甚至是有效

指针值)等等。


自己明确初始化变量总是更好,甚至是静态的变量。如果你将它变成一个函数,你可以通过调用它重新初始化

数组。

或者元素的初始值取决于
版本或C的实现?
Assume I have a static array of structures the elements of which
could be any conceivable mixture of C types, pointers, arrays.
And this array is uninitialized at program startup.

If later in the program I wish to return this array to its
startup state, can this be accomplished by writing binary
zeroes to the entire memory block with memset().
<snip>
No, you can''t, at least not portably. In particular, the all-zeroes
bitpattern is not guaranteed to initialize floating-point variables to
0.0, it is not guaranteed to be a null pointer value (or even a valid
pointer value), etc.

It is always better to explicitly initialize variables yourself, even
static ones. If you turn that into a function, you can reinitialize the
array by calling it.
Or do the initial values of the elements depend on the
version or implementation of C?



编号初始值都是保证默认值:0表示整数,0.0表示

双精度表,空指针表示等等。问题是,你不知道

究竟是用于初始化的比特,所以memset()不会削减它。


S.


No. The initial values are all guaranteed defaults: 0 for ints, 0.0 for
doubles, null pointers for pointers, etc. The problem is, you don''t know
exactly what bits are used for initialization, so memset() won''t cut it.

S.





Charles Sullivan在10/03/05 13:16写道:


Charles Sullivan wrote On 10/03/05 13:16,:
假设我有一个静态的结构数组,其元素可以是任何可能的C类型,指针,数组的混合物。
这个数组在程序启动时未初始化。

如果稍后在程序中我希望将此数组返回到其启动状态,这可以通过使用memset()将二进制零值写入整个内存块来实现。例如,

静态结构mystruct_st {
int x1;
int * x2;
双x3 [10];
- - -
} myarray [20];

/ *用myarray做事* /
- - -
/ *恢复初始myarray * /
memset(我的,0, sizeof(myarray));

(其中 - - - 表示其他有效代码)

或者元素的初始值取决于
C的版本或实现?
Assume I have a static array of structures the elements of which
could be any conceivable mixture of C types, pointers, arrays.
And this array is uninitialized at program startup.

If later in the program I wish to return this array to its
startup state, can this be accomplished by writing binary
zeroes to the entire memory block with memset(). E.g.,

static struct mystruct_st {
int x1;
int *x2;
double x3[10];
- - -
} myarray[20];

/* Do things with myarray */
- - -
/* Restore initial myarray */
memset(my, 0, sizeof(myarray));

(where "- - -" indicates other valid code)

Or do the initial values of the elements depend on the
version or implementation of C?



静态变量永远不会未初始化。在C.它

可能缺少一个明确的初始化程序,或者它可能有一个

初始化程序,它省略了它的一些组成元素,

但是如果所以那些未初始化的块被初始化

到适当类型的零。也就是说,每个`int''都是

初始化为'0'',每个'unsigned int''到'0u'',每个'b $ b'浮点'到' `0.0f'',每个指针指向`(WhateverType *)0'',

等等。


但是有一个问题:C没有详细说明

如何表示值。对于各种整数类型

C承诺使用all-bits-zero填充适当大小(和 - 对齐)的
内存区域会产生零值。 />
但是,对于'float'',`double'',

`long double''或指针类型没有这样的保证。至少有可能

所有位零不是'0.0f'的有效表示,

`0.0''或'0.0L'' ,并且可能不是有效的空指针。在

很多机器上碰巧这些东西实际上是

表示为全位 - 零 - 它非常方便 - 但是

语言不需要它,并且有(或已经)

机器使用(d)其他表示。


所以:如果所有的东西都是整数,那么你的memset肯定会有用,如果涉及非整数元素,那么很可能(但不是

)。

如果您决定使用memset(),那么您将面临较小的风险;如果您愿意避免风险,请尝试
,尝试以下方法:


void clear_structs(struct mystruct_st * p,size_t n){

const struct mystruct_st empty = {0};

while(n- - > 0)

* p ++ =空;

}


-
呃********* @ sun.com



A static variable is never "uninitialized" in C. It
may lack an explicit initializer, or it may have an
initializer that omits some of its constituent elements,
but if so all those "uninitialized" chunks are initialized
to zeroes of appropriate types. That is, each `int'' is
initialized to `0'', each `unsigned int'' to `0u'', each
`float'' to `0.0f'', each pointer to `(WhateverType*)0'',
and so on.

But there''s a catch: C does not specify very much about
how values are represented. For the various integer types
C promises that filling an appropriately-sized (and -aligned)
region of memory with all-bits-zero produces a zero value.
However, no such guarantee is made for `float'', `double'',
`long double'', or pointer types. It is at least possible
that all-bits-zero is not a valid representation of `0.0f'',
`0.0'', or `0.0L'', and may not be a valid null pointer. On
many machines it happens that these things are in fact
represented as all-bits-zero -- it''s so convenient -- but
the language doesn''t require it, and there are (or have been)
machines that use(d) other representations.

So: Your memset will certainly work if all the things
being zapped are integers, and will probably (but not
certainly) work if there are non-integer elements involved.
If you decide to use memset() you''re running a small risk;
if you''d prefer to avoid the risk, try something like:

void clear_structs(struct mystruct_st *p, size_t n) {
const struct mystruct_st empty = { 0 };
while (n-- > 0)
*p++ = empty;
}

--
Er*********@sun.com


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

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