Say I have a struct with an array inside it:
struct my_struct {
// 128 will be enough for all of my use cases,
// saves me from using malloc.
double values[128];
int value_count;
const char *info;
};
The C spec says about compound initializers:
6.5.2.5.14 102) For example, subobjects without explicit initializers are initialized to zero.
Meaning that if I zero initialize my struct, all of its 128 doubles will be initialized to zero. And if I explicitly initialize only the 3 first doubles, the remaining 125 doubles will be initialized to zero.
struct my_struct s = {
// 3 values explicitly initialized,
// the rest will be initialized to zero.
.values = { 1.0, 2.0, 3.0 },
.value_count = 3,
.info = "sequence of reals",
};
I like the elegance of the designated initializer, but I don't like that I'm potentially wasting so much time clearing memory that I won't use. If I only care about the 3 first doubles, is there a way to make C not zero initialize the 125 remaining doubles? Should I avoid designated initializers if I don't want to waste cycles on memory clearing?