Initializing an Object to All Zeroes

Initializing an object to all zeroes

memset is practically never the right way to do it. And yes, there is a practical difference (see below).

In C++ not everything can be initialized with literal 0 (objects of enum types can't be), which is why in C++ the common idiom is

some_struct s = {};

while in C the idiom is

some_struct s = { 0 };

Note, that in C the = { 0 } is what can be called the universal zero initializer. It can be used with objects of virtually any type, since the {}-enclosed initializers are allowed with scalar objects as well

int x = { 0 }; /* legal in C (and in C++) */

which makes the = { 0 } useful in generic type-independent C code (type-independent macros for example).

The drawback of = { 0 } initializer in C89/90 and C++ is that it can only be used as a part of declaration. (C99 fixed this problem by introducing compound literals. Similar functionality is coming to C++ as well.) For this reason you might see many programmers use memset in order to zero something out in the middle of C89/90 or C++ the code. Yet, I'd say that the proper way to do is still without memset but rather with something like

some_struct s;
...
{
const some_struct ZERO = { 0 };
s = ZERO;
}
...

i.e. by introducing a "fictive" block in the middle of the code, even though it might not look too pretty at the first sight. Of course, in C++ there's no need to introduce a block.

As for the practical difference... You might hear some people say that memset will produce the same results in practice, since in practice the physical all-zero bit pattern is what is used to represent zero values for all types. However, this is generally not true. An immediate example that would demonstrate the difference in a typical C++ implementation is a pointer-to-data-member type

struct S;
...

int S::*p = { 0 };
assert(p == NULL); // this assertion is guaranteed to hold

memset(&p, 0, sizeof p);
assert(p == NULL); // this assertion will normally fail

This happens because a typical implementation usually uses the all-one bit pattern (0xFFFF...) to represent the null pointer of this type. The above example demonstrates a real-life practical difference between a zeroing memset and a normal = { 0 } initializer.

C++ Zero-Initialization

The following

MyTest testObj = {};

is not zero-initialization for MyTest, but is simply calling its default constructor. The cppreference page explains why (emphasis mine):

As part of value-initialization sequence for non-class types and for members of value-initialized class types that have no constructors, including value initialization of elements of aggregates for which no initializers are provided.

MyTest is a class type, and a has a constructor.


Defining the constructor with

MyTest() = default;

will instead zero-initialize the object.

Relevant Standard quotes (emphasis mine) below.

From [dcl.init#8]:

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type with either no default constructor ([class.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;

  • if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;

  • ...

From [dcl.init.list]:

List-initialization of an object or reference of type T is defined as follows:

  • ...

  • Otherwise, if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.

How to make sure your object is zero-initialized?

In my humble opinion, the simplest way to ensure zero-initialization is to add a layer of abstraction:

class MyClass
{
struct
{
int a;
int b;
int c;
} data{};
public:
MyClass(int a) : data{a} {}
};

Moving the data members into a struct lets us use value-initialization to perform zero-initialization. Of course, it is now a bit more cumbersome to access those data members: data.a instead of just a within MyClass.
A default constructor for MyClass will perform zero-initialization of data and all its members because of the braced-initializer for data. Additionally, we can use aggregate-initialization in the constructors of MyClass, which also value-initializes those data members which are not explicitly initialized.

The downside of the indirect access of the data members can be overcome by using inheritance instead of aggregation:

struct my_data
{
int a;
int b;
int c;
};

class MyClass : private my_data
{
MyClass() : my_data() {}
public:
MyClass(int a) : MyClass() { this->a = a; }
};

By explicitly specifying the base-initializer my_data(), value-initialization is invoked as well, leading to zero-initialization. This default constructor should probably be marked as constexpr and noexcept. Note that it is no longer trivial. We can use initialization instead of assignment by using aggregate-initialization or forwarding constructors:

class MyClass : private my_data
{
public:
MyClass(int a) : my_data{a} {}
};

You can also write a wrapper template that ensures zero-initialization, thought the benefit is disputable in this case:

template<typename T>
struct zero_init_helper : public T
{
zero_init_helper() : T() {}
};

struct my_data
{
int a;
int b;
int c;
};

class MyClass : private zero_init_helper<my_data>
{
public:
MyClass(int a) { this->a = a; }
};

Having a user-provided constructor, zero_init_helper no longer is an aggregate, hence we cannot use aggregate-initialization any more. To use initialization instead of assignment in the ctor of MyClass, we have to add a forwarding constructor:

template<typename T>
struct zero_init_helper : public T
{
zero_init_helper() : T() {}

template<typename... Args>
zero_init_helper(Args&&... args) : T{std::forward<Args>(args)...} {}
};

class MyClass : private zero_init_helper<my_data>
{
public:
MyClass(int a) : zero_init_helper(a) {}
};

Constraining the constructor template requires some is_brace_constructible trait, which is not part of the current C++ Standard. But this already is a ridiculously complicated solution to the problem.


It is also possible to implement your option #1 as follows:

class MyClass
{
int a;
int b;
int c;

MyClass() = default; // or public, if you like

public:
MyClass(int a)
{
*this = MyClass{}; // the explicitly defaulted default ctor
// makes value-init use zero-init
this->a = a;
}
};

What about constructor delegation?

class MyClass
{
int a;
int b;
int c;

MyClass() = default; // or public, if you like

public:
MyClass(int a) : MyClass() // ctor delegation
{
this->a = a;
}
};

[class.base.init]/7 suggests that the above example shall invoke value-initialization, which leads to zero-initialization since the class does not have any user-provided default constructors [dcl.init]/8.2. Recent versions of clang++ seem to zero-initialize the object, recent versions of g++ do not. I've reported this as g++ bug #65816.

What does {0} mean when initializing an object?

What's happening here is called aggregate initialization. Here is the (abbreviated) definition of an aggregate from section 8.5.1 of the ISO spec:

An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions.

Now, using {0} to initialize an aggregate like this is basically a trick to 0 the entire thing. This is because when using aggregate initialization you don't have to specify all the members and the spec requires that all unspecified members be default initialized, which means set to 0 for simple types.

Here is the relevant quote from the spec:

If there are fewer initializers in the list than there are members in the
aggregate, then each member not
explicitly initialized shall be
default-initialized.
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.

You can find the complete spec on this topic here

How to initialize a struct to 0 in C++

Before we start:

  1. Let me point out that a lot of the confusion around this syntax comes because in C and C++ you can use the = {0} syntax to initialize all members of a C-style array to zero! See here: https://en.cppreference.com/w/c/language/array_initialization. This works:

    // z has type int[3] and holds all zeroes, as: `{0, 0, 0}`
    int z[3] = {0};

    But, that syntax does not work the same for structs, which are entirely different animals than C-style arrays.

  2. See also my follow-up question I asked after writing this answer below: Why doesn't initializing a C++ struct to = {0} set all of its members to 0?


Back to the answer:

I figured it out: to get it to compile, just delete the zero:

# does NOT work
myStruct _m1 = {0};

# works!
myStruct _m1 = {};

It now compiles. However, I ran a bunch of tests to check some things in my struct_initialization.cpp file in my eRCaGuy_hello_world repo, and that does NOT initialize all elements of the struct to zero! Rather, it initializes the struct to its default values. To run my tests and see for yourself, clone my repo above and run eRCaGuy_hello_world/cpp/run_struct_initialization.sh.

Assuming you have this struct:

typedef struct
{
int num1 = 100;
int num2 = -100;
int num3;
int num4 = 150;
} data_t;

Note: the typedef above is a carry-over from when I was testing this stuff in C instead of C++ (although the default struct values are not allowed in C, of course). For C++, this is preferred instead:

struct data_t
{
int num1 = 100;
int num2 = -100;
int num3;
int num4 = 150;
};

So please ignore it wherever I unnecessarily use typedef to define the structs below.

Anyway, if I declare one of the above data_t structs, and then do this:

data_t d2 = {};
printf("d2.num1 = %i\nd2.num2 = %i\nd2.num3 = %i\nd2.num4 = %i\n\n",
d2.num1, d2.num2, d2.num3, d2.num4);

...the output will be:

d2.num1 = 100
d2.num2 = -100
d2.num3 = 0
d2.num4 = 150

And I'm not even sure if d2.num3 is zero because it was initialized to zero or because it was left uninitialized, and that memory location happened to contain zero.

As explained here: https://en.cppreference.com/w/cpp/language/zero_initialization, you can also do this:

myStruct _m1{};

In the example above, this code:

data_t d2{};
printf("d2.num1 = %i\nd2.num2 = %i\nd2.num3 = %i\nd2.num4 = %i\n\n",
d2.num1, d2.num2, d2.num3, d2.num4);

...would produce output identical to what I showed above.

Even in cases where setting the struct to = {0} DOES work, such as this:

// Does NOT do what I expected! Only sets the FIRST value in the struct to zero! 
// The rest seem to use default values.
data_t d3 = {0};
printf("d3.num1 = %i\nd3.num2 = %i\nd3.num3 = %i\nd3.num4 = %i\n\n",
d3.num1, d3.num2, d3.num3, d3.num4);

...the output is still not what I expected, as it only sets the first value to zero! (I don't understand why):

d3.num1 = 0
d3.num2 = -100
d3.num3 = 0
d3.num4 = 150

On C-style arrays, however (NOT structs), these semantics work fine. Refer to this answer here (How to initialize all members of an array to the same value?). The following lines, therefore, both set all elements of the C-style array to zero when using C++:

uint8_t buffer[100] = {0}; // sets all elements to 0 in C OR C++
uint8_t buffer[100] = {}; // sets all elements to 0 in C++ only (won't compile in C)

So, after much experimentation, it looks like the following several ways are the ONLY ways to zero-initialize a struct, PERIOD. If you know differently, please comment and/or leave your own answer here.

The only ways possible to zero-initialize a struct in C++ are:

  1. Be explicit:

     // C-style typedef'ed struct
    typedef struct
    {
    int num1 = 100;
    int num2 = -100;
    int num3;
    int num4 = 150;
    } data_t;

    // EXPLICITLY set every value to what you want!
    data_t d1 = {0, 0, 0, 0};
    // OR (using gcc or C++20 only)
    data_t d2 = {.num1 = 0, .num2 = 0, .num3 = 0, .num4 = 0};
  2. Use memset() to force all bytes to zero:

     data_t d3;
    memset(&d3, 0, sizeof(d3));
  3. Set all default values to zero in the first place:

     // C-style typedef'ed struct
    typedef struct
    {
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    int num4 = 0;
    } data_t;

    // Set all values to their defaults, which are zero in
    // this case
    data_t d4 = {};
    // OR
    data_t d5{}; // same thing as above in C++

    // Set the FIRST value only to zero, and all the rest
    // to their defaults, which are also zero in this case
    data_t d6 = {0};
  4. Write a constructor for the C++ struct

     // 1. Using an initializer list
    struct data
    {
    int num1;
    int num2;
    int num3;
    int num4;

    data() :
    num1(0),
    num2(0),
    num3(0),
    num4(0) {}
    };

    data d7; // all values are zero

    // OR: 2. manually setting the values inside the constructor
    struct data
    {
    int num1;
    int num2;
    int num3;
    int num4;

    data()
    {
    num1 = 0;
    num2 = 0;
    num3 = 0;
    num4 = 0;
    }
    };

    data d8; // all values are zero
  5. Use a struct with no default values, and make your object you create from it static

     typedef struct
    {
    int num1;
    int num2;
    int num3;
    int num4;
    } data_t;

    // `static` forces a default initialization of zero for each
    // value when no other default values are set
    static data_t d9;
  6. So, if you have a struct with non-zero default values, and you want to zero all values, you must do it EXPLICITLY! Here are some more ways:

     // 1. Have a `constexpr` copy of the struct that you use to
    // reset other struct objects. Ex:

    struct data
    {
    int num1 = 1;
    int num2 = 7;
    int num3 = -10;
    int num4 = 55;
    };

    constexpr data DATA_ALL_ZEROS = {0, 0, 0, 0};

    // Now initialize d13 to all zeros using the above `constexpr` struct
    // object
    data d13 = DATA_ALL_ZEROS;

    // OR 2. Use a `zero()` member function to zero the values:

    struct data
    {
    int num1 = 1;
    int num2 = 7;
    int num3 = -10;
    int num4 = 55;

    zero()
    {
    num1 = 0;
    num2 = 0;
    num3 = 0;
    num4 = 0;
    }
    };

    data d14;
    d14.zero();

The big take-away here is that NONE of these: data_t d{}, data_t d = {}, and data_t d = {0}, actually set all members of a struct to zero!

  1. data_t d{} sets all values to their defaults defined in the struct.
  2. data_t d = {} also sets all values to their defaults.
  3. And data_t d = {0} sets only the FIRST value to zero, and all other values to their defaults.

SO, BE EXPLICIT!

Note that the above key take-aways I wrote seem to contradict this documentation on cppreference.com, so it has led me to ask this follow-up question listed just below, which has proven VERY helpful to my understanding!

Going further

  1. MOST USEFUL: Follow-up question of mine: Why doesn't initializing a C++ struct to = {0} set all of its members to 0?

References:

  1. VERY USEFUL:
    1. https://en.cppreference.com/w/cpp/language/zero_initialization
    2. https://en.cppreference.com/w/cpp/language/aggregate_initialization
    3. https://en.cppreference.com/w/cpp/language/value_initialization
  2. VERY USEFUL: Initializing all members of an array (not struct) to the same value:
    1. How to initialize all members of an array to the same value?
    2. [gcc only] How to initialize all members of an array to the same value?
  3. https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/cpp/struct_initialization.cpp
    1. Clone this repo and run the code yourself with cpp/run_struct_initialization.sh

Related:

  1. Initializing default values in a struct
  2. *****[my own answer, which demonstrate this sort of struct modification/aggregate member reassignment within any function: leds[0] = {10, 20, 30, 40, 50};] Arduino Stack Exchange: Initializing Array of structs

Constructor to specify zero-initialization of all builtin members?

You can use a subobject to initialize en masse. A member works, but then you need to qualify all access. So inheritance is better:

struct MoneyData
{
double amountP, amountG, totalChange;
int twenty, ten, five, one, change;
int quarter, dime, nickel, penny;
};

struct Money : MoneyData
{
void foo();
Money() : MoneyData() {} /* value initialize the base subobject */
};

Demonstration (placement new is used to make sure the memory is non-zero before object creation): http://ideone.com/P1nxN6

Contrast with a slight variation on the code in the question: http://ideone.com/n4lOdj

In both of the above demos, double members are removed to avoid possible invalid/NaN encodings.



Related Topics



Leave a reply



Submit