On 05/11/2017 10:34 AM, Jakub Jelinek wrote:
On Thu, May 11, 2017 at 10:23:48AM -0600, Martin Sebor wrote:
Unlike in C, the preferred way to initialize objects in C++
is to use some form of initialization (as opposed to memset).
The preferred way to copy objects is using the copy ctor or
assignment operator (as opposed to memcpy).  Using the special
member functions is clearer, less prone to mistakes and so
safer, and in some cases can also be more efficient.  Memcpy
and memset should be reserved for manipulating raw storage,
not typed objects.

But optimizers should be able to turn the copy ctors/assignment operators
or default ctors into memcpy or memset where possible, making
it efficient again.  And that is something that doesn't work well yet.

I was referring to GCC not expanding inline calls to memcpy with
non-const size, as in function g below, whereas in f, the loop is
fully inlined for (likely) better performance.  This is meant to
be close to the GDB example of a vector-like class that uses
memcpy (or memmove) instead of ordinary assignment.

  struct S { char a[32]; };

  void f (S *p, const S *q, unsigned n)
  {
    while (n--)
      *p++ = *q++;
  }

  void g (S *p, const S *q, unsigned n)
  {
    __builtin_memcpy (p, q, sizeof *q * n);
  }

Martin

Reply via email to