Article View: comp.std.c++
Article #3538Re: Semantics of "new foo[0]"
From: Steve Clamage
Date: Wed, 28 Feb 1996 16:16
Date: Wed, 28 Feb 1996 16:16
49 lines
2386 bytes
2386 bytes
In article 96Feb27175316@kronecker.mitre.org, mgb@kronecker.mitre.org (G. Mike Butler D054) writes:
>According to the ARM section 5.3.3:
> "operator new() can be called with the argument
> zero. Repeated such calls return pointers to
> distinct objects."
>But when I execute the following:
> #include <isotream.h>
> main()
> {
> struct foo { char a[1024]; };
> foo *p = new foo[0];
> foo *q = new foo[0];
>
> cout << (void *)p << " " << (void *)q << endl;
> }
>I get
> 0x338d8 0x338e8
>While these are distinct pointers, the pointers refer to overlapping
>objects.
No, they don't. You asked for an array of zero elements. For an array A
of n elements, you may access any elements from A[0] through A[n-1].
In addition, the address of A[n] is a valid address for comparisons,
but you may not access any object at that address, because there isn't
one at that address. Substitute 0 for n, and you find that the
expression A[i] is invalid for all values of 'i', but &A[0] is a valid
address expression, as long as you don't try to access the (non-existant)
object at that address.
Therefore, as long as the values of p and q are different, all
requirements have been satisfied. You can't usefully describe them as
pointing to "overlapping objects". There are no objects at all, and
you can't dereference the pointers.
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<4h1up3$l69@engnews1.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<MGB.96Feb27175316@kronecker.mitre.org>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: clamage@Eng.sun.com (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Semantics of "new foo[0]" Date: 28 Feb 1996 16:16:48 GMT Organization: Sun Microsystems Inc. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <4h1up3$l69@engnews1.Eng.Sun.COM> References: <MGB.96Feb27175316@kronecker.mitre.org> Reply-To: clamage@Eng.sun.com NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com Content-Length: 1427 X-Lines: 41 Originator: clamage@taumet