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.
The
bzero
function is another option.The only practical difference is that the
={0};
syntax is a bit clearer about saying "initialize this to be empty" (at least it seems clearer to me).Purely theoretically, there are a few situations in which
memset
could fail, but as far as I know, they really are just that: theoretical. OTOH, given that it's inferior from both a theoretical and a practical viewpoint, I have a hard time figuring out why anybody would want to usememset
for this task.I think the initialization speaks much clearer what you actually are doing. You are initializing the struct. When the new standard is out that way of initializing will get even more used (initializing containers with {} is something to look forward to). The memset way are slightly more error prone, and does not communicate that clearly what you are doing. That might not account for much while programming alone, but means a great deal when working in a team.
For some people working with c++, memset, malloc & co. are quite esoteric creatures. I have encountered a few myself.
In C I prefer using {0,} to the equivalent memset(). However gcc warns about this usage :( Details here: http://www.pixelbeat.org/programming/gcc/auto_init.html
In C++ they're usually equivalent, but as always with C++ there are corner cases to consider (noted in other answers).
If the struct contains pointers, the value of all bits zero as produced by
memset
may not mean the same as assigning a0
to it in the C (or C++) code, i.e. aNULL
pointer.(It might also be the case with
floats
anddoubles
, but that I've never encountered. However, I don't think the standards guarantee them to become zero withmemset
either.)Edit: From a more pragmatic perspective, I'd still say to not use
memset
when possible to avoid, as it is an additional function call, longer to write, and (in my opinion) less clear in intent than= { 0 }
.I've never understood the mysterious goodness of setting everything to zero, which even if it is defined seems unlikely to be desirable. As this is tagged as C++, the correct solution to initialisation is to give the struct or class a construtor.