Article View: comp.std.c++
Article #3232Re: new T[0] and sizeof(T)
From: Steve Clamage
Date: Mon, 05 Feb 1996 17:24
Date: Mon, 05 Feb 1996 17:24
50 lines
3138 bytes
3138 bytes
fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes: >clamage@Eng.Sun.COM (Steve Clamage) writes: >>The expression "new T[0]" is invalid, but if you >>use a non-const expression for the count, it is OK if the value is zero. >What makes you think that the expression `new T[0]' is invalid? >As I read the working paper, it seems perfectly legal, and the >three compilers I tried it on were all quite happy to accept it. I now believe I was wrong, and "new T[0]" is valid. Here's the background, if anyone is interested. A while back this question arose in another context and I studied the then-current draft very carefully. I came to the conclusion that "new T[0]" violated some explicit requirements. (I don't remember which version of the draft that was, but that is moot anyway.) When the question came up this time, I checked the "New" section of the current draft (5.3.4) and found a sentence that says that the value of any constant-expression must be strictly positive. In addition, the "Declarators" chapter of the draft has always said that an array type may be declared only if the number of elements is greater than zero. So I assumed that my earlier analysis was correct. Fergus posted an opinion that "new T[0]" was allowed, and I also received some private email on the subject, so I thought I should check more carefully. I re-read that paragraph and discovered the "strictly-positive" requirement doesn't apply to the first dimension of the array, but only to the second and subsequent dimensions. The first dimension must be non-negative. Then I checked with two people who deal with these sorts of issues in the C++ Committee. One at first made the same argument I did, but agreed when I showed how it didn't apply. Both believed this subject had never been explicitly discussed. We agreed that it was inconsistent to allow "new T[n]" when n==0 (which has always been allowed) but not "new T[0]". There also don't seem to be any technical difficulties in allowing the expression. (The new-expression yields type T*, not T[0].) -- Steve Clamage, stephen.clamage@eng.sun.com --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID:
<4f078q$kbh@engnews1.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernReferences:
<1996Feb1.091641.4676@iiasa.ac.at> <4erovm$4hh@engnews1.Eng.Sun.COM> <4evjke$87h@mulga.cs.mu.OZ.AU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: new T[0] and sizeof(T) Date: 05 Feb 1996 09:24:46 PST Organization: Sun Microsystems Inc., Mountain View, CA Approved: austern@isolde.mti.sgi.com Message-ID: <4f078q$kbh@engnews1.Eng.Sun.COM> References: <1996Feb1.091641.4676@iiasa.ac.at> <4erovm$4hh@engnews1.Eng.Sun.COM> <4evjke$87h@mulga.cs.mu.OZ.AU> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 3 Feb 1996 17:50:18 GMT X-Newsreader: NN version 6.5.0 #21 (NOV) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMRY9aEy4NqrwXLNJAQF0PQIAwwACfEr0jqyRfvUU7lbmajedqPj9+dhL VP9ap7SxCx0f9xZ6L2GyU4HqiVZSjHSFo3zgk3AbyAhArNre3AVsOw== =z6z4 Originator: austern@isolde.mti.sgi.com