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

--- Comment #39 from rguenther at suse dot de <rguenther at suse dot de> ---
On Tue, 16 Jun 2020, andrew2085 at gmail dot com wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95349
> 
> --- Comment #38 from Andrew Downing <andrew2085 at gmail dot com> ---
> >  int *p;
> >  int x;
> >  if (<condition>)
> >    p = &x;
> >  else
> >    p = malloc (4);
> >  memcpy (p, q, 4);
> > 
> > there is a single memcpy call and the standard says that both the dynamic
> > type transfers (from q) and that it does not (to x).
> 
> I would say just that, that it both does and doesn't transfer the effective
> type. Meaning that you need to be conservative during optimization and 
> consider
> p to alias both int and whatever type q is.
> 
> > Note the C++ standard makes the placement new optional.  Do you say that
> > your example is incorrect with the placement new elided?
> 
> I'm not sure what you mean about the first part about it being optional. It

Somewhere the C++ standard says (or said in some "old" version) that
the lifetime of an object ends when "... or the storage is re-used".
Likewise lifetime of an object starts "when storage with the proper
alignment and size ... is obtained".  Back in time when I designed
the way GCC currently works to satisfy placement new and friends I
concluded the safe thing to do is to treat every memory write
as changing the dynamic type of the memory location (because we have
to assume it is re-use of storage).

Thus for types without a non-trivial ctor/dtor you do not need to use
placement new.  So take your example and remove the placement new.
Does that change its semantics?

Reply via email to