Starting with gcc 8, compiler is warning about several usages of memset in case of types that are
non-trivially copyable. This includes structs inheriting struct ZeroInit, usage of FillMemory and ZeroMemory macros (defined in pal.h) and other places which are directly calling the function (although some of those places can make use of the defined macro as it stands).
From release notes (https://www.gnu.org/software/gcc/gcc-8/changes.html):
-Wclass-memaccess warns when objects of non-trivial class types are manipulated in potentially unsafe ways by raw memory functions such as memcpy, or realloc. The warning helps detect calls that bypass user-defined constructors or copy-assignment operators, corrupt virtual table pointers, data members of const-qualified types or references, or member pointers. The warning also detects calls that would bypass access controls to data members.
LLVM has included -Wnontrivial-memaccess for C structs and there are also considerations to add -Wclass-memaccess in future: https://reviews.llvm.org/D45310
Reading the community discussions about practices pertaining to usage of memset with non-POD types and how others have responded to the gcc warning, I think there are three ways to fix this (in descending order of preference):
- Call proper ctor, use
std:fill and friends for non-trivial data structures.
- Cast pointer to
void* to circumvent the type check.
- Suppress warning in cmake
-Wno-class-memaccess.
Starting with gcc 8, compiler is warning about several usages of
memsetin case of types that arenon-trivially copyable. This includes
structs inheritingstruct ZeroInit, usage ofFillMemoryandZeroMemorymacros (defined inpal.h) and other places which are directly calling the function (although some of those places can make use of the defined macro as it stands).From release notes (https://www.gnu.org/software/gcc/gcc-8/changes.html):
LLVM has included
-Wnontrivial-memaccessfor C structs and there are also considerations to add-Wclass-memaccessin future: https://reviews.llvm.org/D45310Reading the community discussions about practices pertaining to usage of
memsetwith non-POD types and how others have responded to the gcc warning, I think there are three ways to fix this (in descending order of preference):std:filland friends for non-trivial data structures.void*to circumvent the type check.-Wno-class-memaccess.