Oftentimes data structures' valid initialization is to set all members to zero. Even when programming in C++, one may need to interface with an external API for which this is the case.
Is there any practical difference between:
some_struct s;
memset(&s, 0, sizeof(s));
and simply
some_struct s = { 0 };
Do folks find themselves using both, with a method for choosing which is more appropriate for a given application? (Hopefully it is understood that this is only currently applicable to POD structures; you'd get all sorts of havoc if there was a C++ std::string in that structure.)
For myself, as mostly a C++ programmer who doesn't use memset much, I'm never certain of the function signature so I find the second example is just easier to use in addition to being less typing, more compact, and maybe even more obvious since it says "this object is initialized to zero" right in the declaration rather than waiting for the next line of code and seeing, "oh, this object is zero initialized."
When creating classes and structs in C++ I tend to use initialization lists; I'm curious about folks thoughts on the two "C style" initializations above rather than a comparison against what is available in C++ since I suspect many of us interface with C libraries even if we code mostly in C++ ourselves.
Edit: Neil Butterworth posed this question, in followup, that I believe is an interesting corollary to this question.
some_struct s = { 0 };
is guaranteed to work;memset
relies on implementation details and is best avoided.No you won't. If you use
memset
on such, at the best you will just crash, and at the worst you get some gibberish. The= { }
way can be used perfectly fine on non-POD structs, as long as they are aggregates. The= { }
way is the best way to take in C++. Please note that there is no reason in C++ to put that0
in it, nor is it recommended, since it drastically reduces the cases in which it can be usedThe first will not do what you want: It will try to create a
std::string
from a C-string given a null pointer to its constructor. The second, however, does what you want: It creates an empty string.memset
is practically never the right way to do it. And yes, there is a practical difference (see below).In C++ not everything can be initialized with literal
0
(objects of enum types can't be), which is why in C++ the common idiom iswhile in C the idiom is
Note, that in C the
= { 0 }
is what can be called the universal zero initializer. It can be used with objects of virtually any type, since the{}
-enclosed initializers are allowed with scalar objects as wellwhich makes the
= { 0 }
useful in generic type-independent C code (type-independent macros for example).The drawback of
= { 0 }
initializer in C89/90 and C++ is that it can only be used as a part of declaration. (C99 fixed this problem by introducing compound literals. Similar functionality is coming to C++ as well.) For this reason you might see many programmers usememset
in order to zero something out in the middle of C89/90 or C++ the code. Yet, I'd say that the proper way to do is still withoutmemset
but rather with something likei.e. by introducing a "fictive" block in the middle of the code, even though it might not look too pretty at the first sight. Of course, in C++ there's no need to introduce a block.
As for the practical difference... You might hear some people say that
memset
will produce the same results in practice, since in practice the physical all-zero bit pattern is what is used to represent zero values for all types. However, this is generally not true. An immediate example that would demonstrate the difference in a typical C++ implementation is a pointer-to-data-member typeThis happens because a typical implementation usually uses the all-one bit pattern (
0xFFFF...
) to represent the null pointer of this type. The above example demonstrates a real-life practical difference between a zeroingmemset
and a normal= { 0 }
initializer.The best method for clearing structures is to set each field individually:
In the above example, a
clear
method is defined to set all the members to a known state or value. Thememset
andstd::fill
methods may not work due tostd::string
anddouble
types. A more robust program clears each field individually.I prefer having a more robust program than spending less time typing.
Depending on the compiler optimization, there may be some threshold above which memset is faster, but that would usually be well above the normal size of stack based variables. Using memset on a C++ object with a virtual table is of course bad.
I found a good solution to be:
This does the right thing not only for fundamental types and user-defined types, but it also zero-initializes types for which the default constructor is defined but does not initialize all member variables --- especially classes containing non-trivial
union
members.