🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3372

Re: Correctness of compilers behavior

#3372
From: Ian T Zimmerman
Date: Tue, 20 Feb 1996 18:42
55 lines
2476 bytes
In article <4g3i93$9lp@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU
(Fergus Henderson) writes:

> 
> "Constantine Antonovich:" <const@Orbotech.Co.IL> writes:
> 
> 
> >  for (unsigned i=0; i<size; ++i) {
> >    (bptr+i)->~B();
> >    new(aptr+i) A;
> >  }
> >

> >int main(void)
> >{
> >  A* arr=foo(2);
> >  delete [] arr;
> 
> This has undefined behaviour.  It contravenes 5.3.5[expr.delete]/2, which
> says that the expression passed to `delete []' must be a pointer to the
> first element of an array of objects allocated with `new []'; this is not
> the case, because although there once was such an array at that memory
> location, its lifetime ended when the memory was overwritten by the calls
> to placement new (see 3.8[basic.life]/1).
> 

Is it relevant here that A and B are unrelated classes? What about the
following code?

A a;

A* as = new A [10];

for (int i = 0; i < 10; ++i) {
    new (&as[i]) A(a);
}
delete [] as;

If this is undefined, how else can I initialize array members with a
nondefault constructor?

Thanks in advance,

-- 
Ian T Zimmerman            +-------------------------------------------+
P.O. Box 13445             I    With so many executioners available,   I
Berkeley, California 94712 I suicide is a really foolish thing to do.  I
USA  <itz@rahul.net>       +-------------------------------------------+
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std.c++-request@ncar.ucar.edu 
]
Technical Details
Message-ID: <199602180416.UAA01735@kronstadt.rahul.net>
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
References: <3122157A.2EB2@orbotech.co.il> <4g3i93$9lp@mulga.cs.mu.OZ.AU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
From: Ian T Zimmerman <itz@rahul.net>
Newsgroups: comp.std.c++
Subject: Re: Correctness of compilers behavior
Date: 20 Feb 1996 10:42:51 PST
Organization: -
Approved: austern@isolde.mti.sgi.com
Message-ID: <199602180416.UAA01735@kronstadt.rahul.net>
References: <3122157A.2EB2@orbotech.co.il> <4g3i93$9lp@mulga.cs.mu.OZ.AU>
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: Sat, 17 Feb 1996 20:16:45 -0800
In-Reply-To: fjh@munta.cs.mu.OZ.AU's message of 18 Feb 1996 02:14:25 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMSoWOEy4NqrwXLNJAQE7sgH+NVfdt7WC1BcIQLoPWa8sulrXIImqs1SJ
 sf5XF42ZrkayRxvjqPZwDtU/U2hwS3GSWZL9R2ma6cg8WdLIJn1IRg==
 =kqze
Originator: austern@isolde.mti.sgi.com