关于 C++ 中的二维数组初始化

发布于 2024-12-02 12:25:57 字数 375 浏览 5 评论 0原文

对于 C++ 中的二维数组初始化这个问题,我得到了不同的答复。

场景如下:

class MyClass {
   private:
      static char my2dArray[10][20];
}; 

char MyClass::my2dArray[10][20] = {{0}};

现在是否将此数组的所有元素初始化为 0?

另外,如果我有以下类型定义:

typedef unsigned char u8;

并且此数组是 static u8 my2dArray[10][20],相同的初始化是否有效?

假设所有元素都必须显式初始化为 0。

I get mixed responses for this question on 2-d array initialization in C++.

Here's the scenario:

class MyClass {
   private:
      static char my2dArray[10][20];
}; 

char MyClass::my2dArray[10][20] = {{0}};

Now does this initialize all elements of this array with 0?

Also, if I have the following type definition:

typedef unsigned char u8;

and this array is static u8 my2dArray[10][20], will the same initialization work?

Assuming it's mandatory that all elements are explicitly initialized to 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-12-09 12:25:57

现在这个数组的所有元素都初始化为 0 吗?

是的
如果初始值设定项列表的元素数量与实际数组不同。然后数组被零填充。

同样的初始化会起作用吗?

是的
同样的流程在这里发挥着作用。

Now does this initialize all elements of this array with 0?

Yes
If the initializer list does not have the same number of elements as the actual array. Then the array is zero filled.

will the same initialization work?

Yes
The same processes is at work here.

樱&纷飞 2024-12-09 12:25:57
类 MyClass {
   私人的:
     静态字符 my2dArray[10][20];
}; 

char MyClass::my2dArray[10][20] = {{0}};

现在这个数组的所有元素都初始化为 0 吗?

有点像。

回想一下,数组的元素不是 char,而是 char[10](不能是 0)。

您在这里所做的是通过为 first 内部数组 ({0}) 提供初始化程序来初始化外部数组,同样仅设置内部数组的 first< /em> 元素到 0)。

然后,您将让“默认”隐式初始化行为接管两个数组中的后续元素。

[n3290: 8.5.1/1]: 聚合是一个数组或一个类(第 9 条)
没有用户提供的构造函数(12.1),没有
大括号或等于初始化器用于非静态数据成员 (9.2),无
私有或受保护的非静态数据成员(第 11 条),无基数
类(第 10 条),并且没有虚函数。

[n3290: 8.5.1/2]: 当聚合由初始化器初始化时
列表,如 8.5.4 中所指定,初始化列表的元素是
被视为聚合成员的初始值设定项,在增加
下标或成员顺序。每个成员都是从
相应的初始化子句。如果初始化子句
需要表达式和缩小转换(8.5.4)来转换
表达式,程序格式错误。 [注意如果
initializer-clause本身就是一个初始化列表,成员是
列表初始化,这将导致递归应用
如果成员是聚合,则遵循本节中的规则。
—尾注 ]

[n3290: 8.5.1/7:] 如果列表中的初始化子句较少
比总数中的成员数多,则每个成员不
显式初始化应从空初始化程序初始化
列表 (8.5.4)。

[n3290: 8.5.4/3:] [..] 否则,如果初始值设定项列表没有
元素,该对象已值初始化。
[..]

[最终草案] 标准实际上包含类似行为的非规范示例:

[n3290: 8.5.1/10]: [..] [ 示例: [..] 另一方面,

浮点 y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } };

初始化y的第一列(视为二维
数组)并将其余部分保留为零。 —结束示例 ]

所以,最终,是的。所有最里面的数组元素都将保存值0;只是您的 {{0}} 实际上并没有做太多事情来实现这一点。


假设所有元素都必须显式初始化为 0。

好吧,由于您的数组具有静态存储持续时间,因此它的内存无论如何都会初始化为零,所以这最终是没有意义的,您不必担心。


此外,如果我有以下类型定义:

typedef unsigned char u8;

这个数组是static u8 my2dArray[10][20],相同的初始化会起作用吗?

是的。类型别名不会改变任何内容:该数组具有与第一个数组完全相同的类型(和存储持续时间),并且适用相同的规则。

class MyClass {
   private:
     static char my2dArray[10][20];
}; 

char MyClass::my2dArray[10][20] = {{0}};

Now does this initialize all elements of this array with 0?

Sort of.

Recall that the elements of your array are not char, but char[10] (which cannot be 0).

What you're doing here is initialising the outer array by providing an initialiser for the first inner array ({0}, again setting only the inner array's first element to 0).

You're then letting the "default", implicit initialization behaviour take over for the subsequent elements in both arrays.

[n3290: 8.5.1/1]: An aggregate is an array or a class (Clause 9)
with no user-provided constructors (12.1), no
brace-or-equal-initializers for non-static data members (9.2), no
private or protected non-static data members (Clause 11), no base
classes (Clause 10), and no virtual functions.

[n3290: 8.5.1/2]: When an aggregate is initialized by an initializer
list, as specified in 8.5.4, the elements of the initializer list are
taken as initializers for the members of the aggregate, in increasing
subscript or member order
. Each member is copy-initialized from the
corresponding initializer-clause. If the initializer-clause is an
expression and a narrowing conversion (8.5.4) is required to convert
the expression, the program is ill-formed. [ Note: If an
initializer-clause is itself an initializer list, the member is
list-initialized, which will result in a recursive application of the
rules in this section if the member is an aggregate.
—end note ]

[n3290: 8.5.1/7:] If there are fewer initializer-clauses in the list
than there are members in the aggregate, then each member not
explicitly initialized shall be initialized from an empty initializer
list
(8.5.4).

[n3290: 8.5.4/3:] [..] Otherwise, if the initializer list has no
elements, the object is value-initialized.
[..]

The [final draft] standard actually contains a non-normative example of a similar behaviour:

[n3290: 8.5.1/10]: [..] [ Example: [..] On the other hand,

float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } };

initializes the first column of y (regarded as a two-dimensional
array) and leaves the rest zero. —end example ]

So, ultimately, yes. All of your innermost array elements will hold the value 0; it's just that your {{0}} didn't actually do very much to make that happen.


Assuming it's mandatory that all elements are explicitly initialized to 0.

Well, since your array has static storage duration, its memory will be zero-initialised anyway, so this is ultimately pointless to bother concerning yourself over.


Also, if I have the following type definition:

typedef unsigned char u8;

and this array is static u8 my2dArray[10][20], will the same initialization work?

Yes. The type alias doesn't change anything: this array has precisely the same type (and storage duration) as the first one, and the same rules apply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文