From -2717570383954470353
X-Google-Thread: f78e5,60dc9044ee8e77d3
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
Path: g2news1.google.com!news4.google.com!news.glorb.com!news.alt.net!comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Delivered-To: std-c++@ucar.edu
From: kanze@gabi-soft.fr
Newsgroups: comp.std.c++
Subject: Re: Strange new/new() problem
Organization: http://groups.google.com
Message-ID: <1106064562.182462.247340@c13g2000cwb.googlegroups.com>
References: <41ec486b$1@solnews.wv.mentorg.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1106064576 18284 127.0.0.1 (18 Jan 2005 16:09:36 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 18 Jan 2005 16:09:36 +0000 (UTC)
User-Agent: G2/0.2
Complaints-To: groups-abuse@google.com
Injection-Info: c13g2000cwb.googlegroups.com; posting-host=62.160.54.162;
   posting-account=qsfl8gwAAABZGaLp2a7FeTfDkJamzWYW
X-Virus-Scanned: by amavisd-new at cs.mu.OZ.AU
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from quoted-printable to 8bit by mulga.cs.mu.OZ.AU id j0IGABBp007741
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
X-Virus-Scanned: by amavisd-new at cs.mu.OZ.AU
Date: Thu, 20 Jan 2005 22:31:31 CST
Xref: g2news1.google.com comp.std.c++:3914

"Fedor G. Pikus" wrote:
> I've run into a little problem with operator new.
> Consider this example:
> struct data
> {
>      int i;
>      int j;
> };
> int main()
> {
>      data** p = new (data*)[5];
> }

> What the programmer wanted is to allocate an array of
> pointers, of course, and the parenthesis are unnecessary but
> arguably make reading the code easier.

Why didn't the programmer write what he wanted, then.  And if
the programmer wanted additional parentheses, he should at least
put them someplace they make sense, e.g.:

data** p = new data (*)[ 5 ] ;

A pointer to an array[ 5 ] of data.  (I can't make any sense out
of something like (data*)[ 5 ].  Applying the parentheses, one
gets a pointer to a data of array[ 5 ].  But that doesn't make
any sense.)

> GCC compiled the code just fine,

Then GCC has a serious bug, because there is a type error in the
program.

> and did what the programmer wanted, but several other
> compilers refused.  Error messages are of two types: either
> it's "cannot assign data* to data**" or "[5] is unexpected".

The latter is only a warning, I hope.

> Comeau falls into the first cathegory, and I usually trust
> Comeau to get it right.  Both messages suggest that these
> compilers interpret this code as the placement new with "data
> *" being the location, even though it's a type, not a
> variable.

I suspect rather that both compilers simply interpret this to
mean what is written.  The type expression for the new is
(data*), so the new operator allocates a single data* and
returns its address.  And then applies the [5] to the pointer
which is returned (which, of course, results in undefined
behavior, but [0] wouldn't).

One could argue for a special rule barring the immediate
application of [] to the results of a new expression -- I can't
think of a case off hand where it is useful.  Orthogonality
would argue against such a hack, of course, but that shouldn't
be the only consideration.

More generally, the whole business of array new versus scalar
new is broken.  But I can't think of any way of fixing it
without also fixing C style arrays (which is what is really
broken), and the amount of code that would break is
unthinkable.  So I guess we're just stuck with it.

> Just for kicks, I tried this modification:
> data* p1 = new (data*)[5];
> on some of the compilers which complained about pointer
> conversion, and now the compilers are happy, program runs, and
> p is NULL.

On a compiler with bounds checking, you would get a runtime
error.  You've allocated a scalar, and returned a pointer to it;
then you add 6 to that pointer and dereference.

> So, is the above (topmost) example legal in C++ or parenthesis
> are actually wrong, not just unnecessary?

The parentheses are legal.  They delimit the type expression of
the new expression.  The total statement is illegal, because
dereferencing a data** results in a data*, not a data*.

> Is the modification legal in C++ (Comeau says yes)?

It's syntactically legal, but results in undefined behavior.

Remember how the [] operator is defined.  new (data*)[5] is the
equivalent of *(new (data*) + 5).

> If the p1 line is legal, what is it doing, how is it parsed?
> "data*" is a location argument to placement new?

No.  It's the type expression.

> What't the type then?

data*.

That is, after all, why you put it in parentheses, no?  So that
the [] wouldn't be part of the type expression.

--
James Kanze           GABI Software         http://www.gabi-soft.fr
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



