3 ms·
How 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
by colin_mccabe 13y ago
How 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.