If the data is a static or global variable, it is zero-filled by default, so just declare it myStruct _m;
If the data is a local variable or a heap-allocated zone, clear it with memset
like:
memset(&m, 0, sizeof(myStruct));
Current compilers (e.g. recent versions of gcc
) optimize that quite well in practice. This works only if all zero values (include null pointers and floating point zero) are represented as all zero bits, which is true on all platforms I know about (but the C standard permits implementations where this is false; I know no such implementation).
You could perhaps code myStruct m = {};
or myStruct m = {0};
(even if the first member of myStruct
is not a scalar).
My feeling is that using memset
for local structures is the best, and it conveys better the fact that at runtime, something has to be done (while usually, global and static data can be understood as initialized at compile time, without any cost at runtime).