[Bug c++/87427] -Wclass-memaccess improvement for POD with convenience initializer
msebor at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Tue Sep 25 06:38:00 GMT 2018
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87427
Martin Sebor <msebor at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |diagnostic
Status|UNCONFIRMED |RESOLVED
CC| |msebor at gcc dot gnu.org
Resolution|--- |WONTFIX
--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
A common C idiom is
struct S *p = (struct S*)malloc (sizeof *p);
memset (p, 0, sizeof *p);
When such code is being ported to C++, or when C programmers are writing
analogous code in C++, it's not unusual to make the mistake of assuming that
the same code will work unchanged even when struct S has a default constructor
(or forget that it does, or add one and forget to change the dynamic
initialization). That's not the case with a struct like the one in the example
in comment #0, and the warning helps detect this mistake. We would not want to
weaken the warning to prevent detecting it.
In our view, memset (and other raw memory functions) are low-level primitives
designed for manipulating object representation at the byte level. They should
not be used with higher-level abstractions. The preferred way to initialize
objects of non-trivial types is by using their ctors, and that's what the
warning is meant to encourage.
More information about the Gcc-bugs
mailing list