开发者

Regarding 2-d array initialisation in C++

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜