C和C ++:自动结构的部分初始化 [英] C and C++ : Partial initialization of automatic structure

查看:170
本文介绍了C和C ++:自动结构的部分初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果 somestruct 有三个整数成员,我一直认为这是确定要做到这一点在C(或C ++)函数:

  somestruct S = {123};

第一个成员将被初始化为123,最后两个将被初始化为0。我经常做同样的事情用自动数组,写 INT ARR [100] = {0,}; 使得数组中的所有整数被初始化为零。

结果
最近,我在 GNU C参考手册是:


  

如果你不初始化结构变量,其效果取决于
  无论是具有静态存储(请参阅存储类型说明符)或
  不。如果是,带有整体类型的成员与0和初始化
  指针成员初始化为NULL;否则,的值
  结构的成员是不确定的。


结果
是否有人可以告诉我是什么C和C ++标准说关于半自动结构和自动数组初始化?我在Visual Studio上述code没有问题,但我想用gcc兼容/ g ++的,也许其他的编译器为好。谢谢


解决方案

链接的GCC文档不说话的部分初始化这只是谈判的(完成)初始化否初始化


  

什么是部分初始化?


该标准没有定义对象的初始化部分,无论是有完整的初始化或不初始化。部分初始化是一个非标准术语这通常指在那里你提供一些初始化,但不是所有的即一种情况:比数组的大小或初始化结构元素的数量较少初始化。

例如:

  int数组[10] = {1,2}; //案例1:部分初始化


  

什么是(完成)初始化或没有初始化?


初​​始化意味着提供一些初始值在被创建时,同时创建该变量的存在。即:在同一个code语句

例如:

  int数组[10] = {0,1,2,3,4,5,6,7,8,9}; //案例2:完成初始化
int数组[10]; //案例3:没有初始化

引用的该段描述的行为案例3

对于部分初始化规则(案例1 )深受标准的定义,这些规则不依赖于变量的存储类型被初始化。结果
据我所知,所有主流的编译器具有100%的遵守这些规则。



  

有人能告诉我是什么C和C ++标准说关于半自动结构和自动初始化数组?


C和C ++标准保证了即使一个整数数组位于自动存储,如果有一个大括号括起来的列表少初始化则初始化元素的必须被初始化为 0

C99标准6.7.8.21


  

如果有一个大括号括起来的列表更少的初始化值多于使用比在数组中的元素来初始化已知大小的数组元素或聚集的成员,或者更少的字符的字符串,则剩余合计应初始化隐含一样具有静态存储持续时间的对象。



在C ++中的规则有差别不大说明。

C ++标准03 8.5.1骨料结果
第7段:


  

如果有列表中的初始化更少比有成员在总体上,那么每个成员没有初始化应值初始化(8.5)。
  [示例:

 的struct {int类型的;字符* B; INT℃; };
 小号SS = {1,ASDF};


  
  

初​​始化 ss.a 1 ss.b ASDF ss.c 与形式的前pression的价值 INT(),即 0 。 ]


虽然值初始化中,结果定义
C ++ 8.5 03的初始化结果
第5段:


  

值初始化类型T的对象是指:结果
   - 如果T是一个类类型(第9条)与用户声明的构造(12.1),则T的默认构造函数(并形成不良的初始化,如果T没有访问
    默认的构造函数);结果
   - 如果T是一个非工会类型不用户声明的构造,那么每个非静态
    数据成员和T的基础类组件是值初始化;结果
   - 如果T是数组类型,那么每一个元素都是值初始化;结果
   - 否则,该目的是零初始化


For example, if somestruct has three integer members, I had always thought that it was OK to do this in C (or C++) function:

somestruct s = {123,};

The first member would be initialized to 123 and the last two would be initialized to 0. I often do the same thing with automatic arrays, writing int arr[100] = {0,}; so that all integers in an array are initialized to zero.


Recently I read in the
GNU C Reference Manual that:

If you do not initialize a structure variable, the effect depends on whether it is has static storage (see Storage Class Specifiers) or not. If it is, members with integral types are initialized with 0 and pointer members are initialized to NULL; otherwise, the value of the structure's members is indeterminate.


Can someone please tell me what the C and C++ standards say regarding partial automatic structure and automatic array initialization? I do the above code in Visual Studio without a problem but I want to be compatible with gcc/g++, and maybe other compilers as well. Thanks

解决方案

The linked gcc documentation does not talk of Partial Initialization it just talks of (Complete)Initialization or No Initialization.

What is partial Initialization?

The standards do not define Partial initialization of objects, either there is Complete initialization or No-initialization. Partial Initialization is a non-standard terminology which commonly refers a situation where you provide some initializers but not all i.e: Fewer initializers than the size of the array or the number of structure elements being initialized.

Example:

int array[10] = {1,2};                    //Case 1:Partial Initialization

What is (Complete)Initialization or No Initialization?

Initialization means providing some initial value to the variable being created at the same time when it is being created. ie: in the same code statement.

Example:

int array[10] = {0,1,2,3,4,5,6,7,8,9};    //Case 2:Complete Initialization
int array[10];                            //Case 3:No Initialization

The quoted paragraph describes the behavior for Case 3.

The rules regarding Partial Initialization(Case 1) are well defined by the standard and these rules do not depend on the storage type of the variable being initialized.
AFAIK, All mainstream compilers have 100% compliance to these rules.


Can someone please tell me what the C and C++ standards say regarding partial automatic structure and automatic array initialization?

The C and C++ standards guarantee that even if an integer array is located on automatic storage and if there are fewer initializers in a brace-enclosed list then the uninitialized elements must be initialized to 0.

C99 Standard 6.7.8.21

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.


In C++ the rules are stated with a little difference.

C++03 Standard 8.5.1 Aggregates
Para 7:

If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized (8.5). [Example:

 struct S { int a; char* b; int c; };
 S ss = { 1, "asdf" };

initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is,0. ]

While Value Initialization is defined in,
C++03 8.5 Initializers
Para 5:

To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

这篇关于C和C ++:自动结构的部分初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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