https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85707

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot 
gnu.org

--- Comment #7 from Martin Sebor <msebor at gcc dot gnu.org> ---
In the test case, the warning is intended because the access in
HashTable::init_table_memset() bypasses the constructor of the Position class. 
If the constructor had other side-effects (e.g., count the number of objects of
the class) bypassing it could violate the invariant (see the example below). 
Detecting this sort of a problem is one the design goals of the warning.

To avoid the warning either cast the argument to void* or access the array via
the address of the Position::index member of the first element of the table_
array:

  memset (&table_->index, 0xff, sizeof(Position) * _buckets);

Alternatively, suppress the warning via #pragma GCC diagnostic.

$ cat pr85707.C && gcc -O2 -S -Wall pr85707.C
struct S
{
  static int count;

  int i;

  S () {
    ++count;
    __builtin_memset (this, 0, sizeof *this);
  } 

  ~S () { --count; }

  void f () { __builtin_memset (this, 0, sizeof *this); }   // no warning (see
pr84850)

  void g (S *p) {
    __builtin_memset (p, 0, sizeof *p);   // -Wclass-memaccess
  }
};

pr85707.C: In member function ‘void S::g(S*)’:
pr85707.C:17:38: warning: ‘void* __builtin_memset(void*, int, long unsigned
int)’ clearing an object of non-trivial type ‘struct S’; use assignment or
value-initialization instead [-Wclass-memaccess]
     __builtin_memset (p, 0, sizeof *p);   // -Wclass-memaccess
                                      ^
pr85707.C:1:8: note: ‘struct S’ declared here
 struct S
        ^

As a side note, in the solution added for pr84850, -Wclass-memaccess was
relaxed to avoid triggering for raw memory accesses to *this in non-static
member functions of non-nontrivial classes with no non-trivial members.  It was
a deliberate decision to avoid applying the solution to accesses to objects
other than *this.

Reply via email to