Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a struct of integers to zero? [duplicate]

Tags:

c++

c++11

What is the best way to make sure the following large struct always has its integers initialized to 0?

struct Statistics {
    int num_queries;
    int num_respones;
    // ... 97 more counters here
    int num_queries_filtered;
}

I would like to avoid having to check each place this struct is initialized to make sure it is value initialized with Statistics s(); rather than default initialized with Statistics s;.

Statistics s;     // Default initialized by accident here
s.num_queries++;  // Oh no, this is a bug because it wasn't initialized to zero
Statistics s2{};  // Correctly value initialized
s2.num_queries++; // Successful

Proposal 1 - Use memset, but this feels like a hack where we take advantage of the value initialization happening to be equivalent to 0 filling the data structure:

struct Statistics {
    Statistics() { memset(this, 0, sizeof(*this)); }
    // ... counters here
}

Proposal 2 - Use constructor initialization lists, but this is cumbersome and when people add new counters in the future they may forget to zero-initialize them in the constructor:

struct Statistics {
    Statistics() : num_queries(0), num_respones(0), /* ... */, num_queries_filtered(0) {}
    // ... counters here
}

Proposal 3 - Force the value initialization to take place as follows:

struct StatisticsUnsafe {
    // ... counters here
}

struct Statistics : public StatisticsUnsafe {
    Statistics() : StatisticsUnsafe() {}
}

What do you feel is the best approach? Do you have other alternatives?

EDIT I want to clarify that in my actual code, each of the counters has a meaningful name, such as "num_queries_received", "num_responses", etc. Which is why I do not opt to use a vector or array of the form "counters[100]"

EDIT2 Changed the example from Statistics s2(); to Statistics s2{};

like image 552
AffluentOwl Avatar asked Oct 09 '14 19:10

AffluentOwl


People also ask

How do you initialize a structure element to zero?

You don't have to initialise every element of a structure, but can initialise only the first one; you don't need nested {} even to initialise aggregate members of a structure. Anything in C can be initialised with = 0 ; this initialises numeric elements to zero and pointers null.

Can you initialize a struct to NULL?

It is not possible to set a struct with NULL as it is declared. Fila f = NUll; error: invalid initializer. So cast% from% to NULL , which is not even a type in this code, or assign Fila to a primitive type variable is "wrong". " NULL is not a value", it is an untyped pointer to address 0.

Are ints automatically initialized to 0?

Instance variables of numerical type (int, double, etc.) are automatically initialized to zero if you provide no other values; boolean variables are initialized to false; and char variables, to the Unicode character with code number zero. An instance variable can also be a variable of object type.

Can you initialize struct values?

An important thing to remember, at the moment you initialize even one object/ variable in the struct, all of its other variables will be initialized to default value. If you don't initialize the values in your struct, all variables will contain "garbage values".


1 Answers

From C++11, you may also do:

struct Statistics {
    int counter1 = 0;
    int counter2 = 0;
    // ... more counters here
    int counter100 = 0;
};
like image 139
Jarod42 Avatar answered Sep 25 '22 18:09

Jarod42