Rule 8-5-2 what is "non-zero initialization"?

Moderators: david ward, misra cpp

Post Reply
Posts: 15
Joined: Wed May 14, 2014 1:02 pm
Company: Axivion GmbH

Rule 8-5-2 what is "non-zero initialization"?

Post by grunwald » Tue Jan 24, 2017 2:21 pm

C++ has the concepts:
  • default initialization ("T var;")
    Calls the default constructor (if it exists); otherwise the memory is left uninitialized.
  • value initialization ("T var = {};")
    Calls the default constructor if it exists and is user-defined.
    Otherwise, performs zero-initialization and then calls the compiler-generated default constructor (if it exists).
  • zero initialization
    Roughly equivalent to a memset(..., 0, ...)
The term "non-zero initialization" is not defined anywhere.

Code: Select all

struct POD { int member1; int member2; };
class UserDefinedCtor { public: UserDefinedCtor(); };
struct CompilerDefinedCtor { int member1; UserDefinedCtor member2; };

POD pod[3] = {}; // value initialization that uses zero initialization
UserDefinedCtor udc[3] = {}; // value initialization but does not involve zero initialization
CompilerDefinedCtor cdc[3] = {}; // value initialization that involves both zero initialization and constructor calls
The declaration `pod` is clearly compliant with Rule 8-5-2.

What about `udc`? If it is classified as "non-zero initialization", Rule 8-5-2 would require an explicit initializator for each element:

Code: Select all

UserDefinedCtor udc[3] = { UserDefinedCtor(), UserDefinedCtor(), UserDefinedCtor() };
But maybe "zero initialization" in this rule does not refer to the C++ concept, but instead to the syntactic forms "{}", "{0}" and "{NULL}" ?

misra cpp
Posts: 154
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 8-5-2 what is "non-zero initialization"?

Post by misra cpp » Wed Jul 12, 2017 2:16 pm

We agree with your interpretation and this will be addressed in the next version
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.8 Declarators (C++)”