[Spice-devel] [PATCH 07/17] Get rid of C-style memset initializations, use C++ style aggregates

Christophe de Dinechin christophe.de.dinechin at gmail.com
Fri Feb 23 11:20:09 UTC 2018



> On 23 Feb 2018, at 12:08, Christophe Fergeau <cfergeau at redhat.com> wrote:
> 
> On Fri, Feb 23, 2018 at 12:01:59PM +0100, Christophe de Dinechin wrote:
>> 
>> 
>>> On 23 Feb 2018, at 10:53, Christophe Fergeau <cfergeau at redhat.com> wrote:
>>> 
>>> Given the lengthy debate over what is mostly a small cosmetic patch, I
>>> suggest that we postpone this one for now and drop it from the series.
>> 
>> memset in C++ code is not just a style issue, it’s dangerous. It completely wipes out C++ type guarantees. For example, if someone inits a field with
>> 
>> 	int x = 1;
>> 
>> Then all constructors will guarantee that x == -1, but a memset after
>> object creation wipes out that guarantee. Same thing if we make of of
>> the objects being memset-initialized contain some C++ object with a
>> vtable. And so on. All these problems do not exist with C++
>> zero-initialization.
> 
> Is this an actual problem with the 2 structs which are being discussed
> here? In other word, is this patch currently fixing a bug? I don't think
> it does, so it can safely be postponed for a later time when people get
> to an agreement on it, or when we have less patches pending, ...
> 
>> Which is also significantly shorter to write.
> 
> I did not mention it the first time, but this patch is added more lines
> that it removes. So I'll beg to disagree with the "shorter" part ;)

Petty, because we were specifically talking about zero-init, i.e.:

	foo x = {};

is shorter than
	foo x;
	memset(&x, 0, sizeof(x));


But since you brought a new point, you counted lines. If count bytes, the first section of my patch is 381 bytes, it was 473 bytes before, so yes, “shorter” in bytes :-) And frankly, I wish I did not have to spend time countering this kind of argument!


Christophe


More information about the Spice-devel mailing list