FrozenHeart
FrozenHeart

Reputation: 20746

How to zero-out the whole array of structure objects which contains other structures and arrays?

How to zero-out the whole array of structure objects which contains other structures, arrays, etc in the more standard way?

In my case it's an object of INPUT structure.

Is the following code do it right?

INPUT newline_input[4] = {0};

Or should I use something like std::memset?

Thanks in advance.

Definition of INPUT

Upvotes: 0

Views: 139

Answers (2)

Surt
Surt

Reputation: 16099

As far as I can see only the first element of the first array element would be set to the value, so only because it is set to zero here would it be correct. If it had been '= {1}' and you expected all elements to be 1, it would fail as the elements would be {1,0,0,0 ... etc. }

I don't like the '=' because it looks like it would call an assignment operator (which is irrelevant for this structure but may be a problem in other cases).

If the array was a global its memory would be zeroed but that is accidental that the wanted values are zero. If the array was on the stack random values (whatever is on the stack) would populate the array.

Initialization in the age of C++14 its a bit more complicated.

INPUT newline_input[4] {}; // empty value initialization

So use empty value initialization for your data which include a union syntax 4 is used

T object {};

and then effect 3 followed by 2(C++14)

3) If T is an array type, each element of the array is value-initialized
2) If T is a class type without a user-provided or deleted default constructor (that is, it may be a class with a defaulted default constructor or with an implicitly-defined one) then the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor   (since C++14)

Which should then call zero-initialization.

These unions with unknown content is a problem when they are provided without constructors as the programmer would have to know how to put them into a well defined state.

Using list initialization looks too much like assignment and

INPUT newline_input[4] = {0};
or
INPUT newline_input[4] = {};

It will use syntax 10 in List initialization followed by aggregate initialization as INPUT is an array/struct/union. And since the number of initializer clauses is less than the array length, this effect will be used

If the number of initializer clauses is less than the number of members or initializer clauses is completely empty, the remaining members are initialized by their brace-or-equal initializers, if provided in the class definition, and otherwise (since C++14) by empty lists, which performs value-initialization. If a member of a reference type is one of these remaining members, the program is ill-formed (references cannot be value-initialized)

But members of the standard comity has moved away from this form [link to where Bjarne S. says so] to the value initializing.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249153

Your code will work, even better is this:

INPUT newline_input[4] = {};

Upvotes: 3

Related Questions