5 ms·
> For example, I like static data structures, especially in C. But in C++ they are unidiomatic. How do you statically initialize (in the rdata / rodata segment)
by thwest 13y ago
> For example, I like static data structures, especially in C. But in C++ they are unidiomatic. How do you statically initialize (in the rdata / rodata segment) an object graph (even as simple as a tree) with zero-copying at runtime?
How do you do that in C? Does unidiomatic mean you can't do it? Interesting challenge.
That's also an interestingly-sized data structure. Apparently big enough that the initialization time is a performance concern, but small enough to be able to lay out statically and not burden your binary size.
- colin_mccabe 13y agoHow do you [zero-initialize a structure] in C? Does unidiomatic mean you can't do it? Interesting challenge. You can zero-initialize an object on the stack in C like this: struct my_object obj = { 0 }; Everything in the structure, no matter what it is, will be 0. Longs will be 0, shorts will be 0, pointers will be null, etc. In C++, you can still do this, but only for POD types. Basically, in C++, it is assumed that you will manually initialize all data members in the constructor. POD objects get a free pass since they're essentially C structs.