对数组初始化很好奇。 [英] curious about array initialization.

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

问题描述

说我的结构里面有一个数组。


例如


struct random_struct {

char name [10];

int month [12];

}


如果数组不是由我初始化的,在某种意义上,我为结构分配了一个

内存,整数数组是否会被初始化为0

所有条目? (例如月[0] - 月[11]都等于0)。


这个编译器是依赖还是随机的?


谢谢

解决方案

问题?写道:

说我的结构里面有一个数组。



struct random_struct {
char name [10] ;
int month [12];
}

如果我没有初始化数组,从某种意义上说,我为结构分配了一个
内存,所有条目的整数数组都被初始化为0
? (例如month [0] -month [11]都等于0)。

这个编译器是依赖还是随机的?

谢谢



我会想象它会依赖于编译器,但我不知道这个标准几乎和这里的很多人一样。


你不能假设你的数组会被初始化为0,所以

特定的原因,你不可能在很大程度上无关紧要。


"问题" <未************ @ hotmail.com>写道:

说我的结构里面有一个数组。



struct random_struct {
char name [10] ;
int month [12];
}

如果我没有初始化数组,从某种意义上说,我为结构分配了一个
内存,所有条目的整数数组都被初始化为0
? (例如,月[0] - 月[11]都等于0)。




如果声明对象具有静态存储持续时间,则所有成员

将被初始化为零(0,0.0,''\ 0'或NULL,具体取决于

类型)。如果它有自动存储持续时间,或者如果用malloc()分配

,初始值将是垃圾 - 这可能只是

碰巧为零。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。




Keith Thompson写道:

" questions?" <未************ @ hotmail.com>写道:

说我的结构里面有一个数组。



struct random_struct {
char name [10] ;
int month [12];
}

如果我没有初始化数组,从某种意义上说,我为结构分配了一个
内存,所有条目的整数数组都被初始化为0
? (例如月[0] - 月[11]都等于0)。



如果声明对象具有静态存储持续时间,则所有成员将被初始化为零(0,0.0,''\ 0''或NULL取决于
类型)。如果它有自动存储持续时间,或者它已经与malloc()一起分配,那么初始值将是垃圾 - 可能只是恰好为零。



静态存储持续时间是什么意思?你是指上面给出的数组

声明吗?


另外,我试图在DevC ++中运行代码4.9.9.2:

#include< stdio.h>


int main(){

int i;


struct random_struct {

char name [10];

int month [12];


} mystruct; < (b =(i = 0; i< 10; i ++){

printf("%d \ t%c \ n),mystruct。
for。 month [i],mystruct.name [i]);

}


getchar();


返回0;

}

------------------------------ ---


这是输出。整数数组和char都没有被初始化为0的
。你不能假设。因此,只需预先初始化

您正在使用的所有内容,以避免任何问题。看看

memset()。我认为memset(& mystruct,0,sizeof(mystruct));应该立即为整个结构做

工作,但要仔细检查。


-1`

2009252814#

2293528


say I have a structure which have an array inside.

e.g.

struct random_struct{
char name[10];
int month[12];
}

if the array is not intialized by me, in a sense after I allocated a
memory for the structure, will the integer array be initialized to 0
for all entries? (e.g. month[0]-month[11] are all equal 0).

Is this compiler dependent or random?

Thanks

解决方案

questions? wrote:

say I have a structure which have an array inside.

e.g.

struct random_struct{
char name[10];
int month[12];
}

if the array is not intialized by me, in a sense after I allocated a
memory for the structure, will the integer array be initialized to 0
for all entries? (e.g. month[0]-month[11] are all equal 0).

Is this compiler dependent or random?

Thanks


I would imagine that it would be compiler-dependant, but I don''t know
the Standard nearly as well as a lot of people here.

You can''t assume that your arrays will be initialized to 0, so the
specific reason that you can''t is for the most part irrelevant.


"questions?" <un************@hotmail.com> writes:

say I have a structure which have an array inside.

e.g.

struct random_struct{
char name[10];
int month[12];
}

if the array is not intialized by me, in a sense after I allocated a
memory for the structure, will the integer array be initialized to 0
for all entries? (e.g. month[0]-month[11] are all equal 0).



If the object is declared with static storage duration, all members
will be initialized to zero (either 0, 0.0, ''\0'', or NULL depending on
the type). If it has automatic storage duration, or if it''s allocated
with malloc(), the initial value will be garbage -- which might just
happen to be zero.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.



Keith Thompson wrote:

"questions?" <un************@hotmail.com> writes:

say I have a structure which have an array inside.

e.g.

struct random_struct{
char name[10];
int month[12];
}

if the array is not intialized by me, in a sense after I allocated a
memory for the structure, will the integer array be initialized to 0
for all entries? (e.g. month[0]-month[11] are all equal 0).



If the object is declared with static storage duration, all members
will be initialized to zero (either 0, 0.0, ''\0'', or NULL depending on
the type). If it has automatic storage duration, or if it''s allocated
with malloc(), the initial value will be garbage -- which might just
happen to be zero.



What do you mean by static storage duration? Are you referring to array
declaration as the one given above?

Also, I have tried to run the code in DevC++ 4.9.9.2:
#include <stdio.h>

int main() {
int i;

struct random_struct{
char name[10];
int month[12];

} mystruct;

for (i=0; i<10; i++) {
printf("%d\t%c\n", mystruct.month[i], mystruct.name[i]);
}

getchar();

return 0;
}
---------------------------------

Here is the output. Neither the integer array, nor char were
initialized to 0. You cannot assume that. So just preinitialize
everything that you are using to avoid any problems. Check out
memset(). I think memset(&mystruct, 0, sizeof(mystruct)); should do the
job for the whole structure at once, but double check that.

-1 `
2009252814 #
2293528


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

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