From -6765025701996471364
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8372ae6de42be399
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2004-02-20 11:54:03 PST
Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!easynet-monga!easynet.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: stephen.clamage@sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Array of size 0
Date: Fri, 20 Feb 2004 19:54:01 +0000 (UTC)
Organization: Sun Microsystems, Inc.
Lines: 54
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <c15mlg$pdl$1@news1nwk.SFbay.Sun.COM>
References: <403601F6.8010706@ircad.u-strasbg.fr>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: mail2news.demon.co.uk 1077306841 27480 10.0.0.1 (20 Feb 2004 19:54:01 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Fri, 20 Feb 2004 19:54:01 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.12)
	id 1AuGim-000795-00
	for mail2news@news.news.demon.net; Fri, 20 Feb 2004 19:54:00 +0000
X-Received: from mulga.cs.mu.OZ.AU (localhost [127.0.0.1]) by mulga.cs.mu.OZ.AU with ESMTP
	id i1KJrvi2002435; Sat, 21 Feb 2004 06:53:57 +1100 (EST)
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i1KJrujb002426;
	Sat, 21 Feb 2004 06:53:56 +1100 (EST)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Path: comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Delivered-To: std-c++@ucar.edu
X-Newsgroups: comp.std.c++
X-NNTP-Posting-Date: Fri, 20 Feb 2004 19:20:16 +0000 (UTC)
X-User-Agent: Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.4) Gecko/20031128
X-Accept-Language: en-US, en
X-Spam-Level: 
X-Spam-Checker-Version: SpamAssassin 2.60-mulga_r1 (1.212-2003-09-23-exp) on 
	mulga.cs.mu.OZ.AU
X-Spam-Status: No, hits=0.0 required=5.2 tests=none autolearn=no 
	version=2.60-mulga_r1
Xref: archiver1.google.com comp.std.c++:1183

Julien Lamy wrote:
> Hello,
> According to paragraph 6 of item 5.3.4 in the Standard, it is legal to
> dynamically allocate an array of size 0, i.e.
> new int[0]
> is legal.
> However, as I understand paragraph 1 of item 8.3.4 ("if the cosntant
> expression is present, [...] its value shall be greater than zero"), it
> is not legal to have
> int myArray[0]
> What is the rationale behind this difference ?
> Thanks in advance,

If you could write
	int myArray[0];
it would have no use. You could not access any elements. In addition, we say 
that C++ has no zero-sized complete objects. We'd have to make a special case 
for myArray[0].

C++ currently allows
	int myArray[n];
only when n is an integer constant expression. So you know at compile time 
whether the array has any elements, and you can take reasonable steps to avoid 
trying to create a fixed array with no elements. If nothing else, you could write
	int myArray[ n<=0 ? 1 : n ];

Now consider
	int* myArray = new int[n];
If n were not allowed to be zero, the runtime system would have to check for 
zero elements and generate some kind of runtime error, probably an exception. 
The program would have to check specially for zero size or be prepared to deal 
with the exception -- perhaps in many places.

If instead the results of the program were undefined, the situation would be 
worse. The program would run on some systems, not on others. A defensive 
programmer would have to treat n==0 as a disallowed case.

Allowing n to be zero is useful. Functions and algorithms become simpler. You 
can process all the elements (if any) of myArray using
	for( int i = 0; i < n; ++n ) { ... myArray[i] ... }
You usually won't need special runtime checks for zero size.

Finally, "new int[0]" is not a special case. It is analagous to malloc(0). In 
both cases you get a valid pointer that you cannot dereference.

---
Steve Clamage, stephen.clamage@sun.com

---
[ 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                       ]



