Davi Arnaut <davi(a)twitter.com> writes:
> On Thu, Jul 26, 2012 at 5:50 PM, Igor Babaev <igor(a)askmonty.org> wrote:
>> + bzero(column_stats, sizeof(Column_statistics) * cnt);
>
> That's not allowed for non-POD types.
Can you elaborate on exactly when bzero() (or memset() or similar) is allowed
or not allowed?
I searched in my C++ reference, and did not really find anything ... in fact I
did not find anything that said memset(p, 0, sizeof(*p)) would _ever_ be
allowed for a class.
It is my understanding that even for C, memset(..., 0, ...) is only valid for
integer types. Initialising with memset() floating-point and even pointers
values is not well-defined! Or did I miss something?
I think this is an important issue, as memset() of a C++ class is certainly
wrong in many cases, and can lead to subtle and platform-dependent
failures. So let's point out clearly the rules that apply, or define such
rules if necessary:
1. Does the C and C++ standards define suitable rules? If so, we should use
them. However, I suspect that memset() is invalid in standard C and C++ except
for arrays of integers, which is excessively restrictive.
2. If C/C++ standard is not sufficient, we need to agree on a coding style
(and stay within what works on all reasonable compilers/platforms).
I would much prefer to find an existing standards rule or convention. But
failing that I would suggest:
- Only memset() and memcpy() structs.
- Virtual functions are definitely out (in struct as well as members).
- Constructors and destructors also must not be defined for such structs (or
its members).
- Derived classes also should never be memset()'ed. Class inheritance is
likely to need extra info on some (present or future) platforms and
compilers.
- Do not have private: methods in such memset() structs. I do not think it
matters to the code generated, but private: is meaningless if arbitrary
code is manipulating the memory of the struct directly. Usually I would
have no methods at all in structs that I plan to do memset() on.
But I suppose we also need to look at what existing code does.
Comments?
- Kristian.