๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

6 total messages Started by G. Mike Butler D054 Tue, 27 Feb 1996 23:13
Semantics of "new foo[0]"
#3528
Author: G. Mike Butler D054
Date: Tue, 27 Feb 1996 23:13
35 lines
1720 bytes
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. Is this a legitimate interpretation of section 5.3.3 or is
this a compiler/library bug?

Incidentally, the standard template library included with gcc 2.7.2
relies on "operator new(0)" working.

Thanks,
-Mike Butler
---
[ 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 
]
Re: Semantics of "new foo[0]"
#3536
Author: Alexandre Oliva
Date: Wed, 28 Feb 1996 16:16
58 lines
2863 bytes
The ARM is  out of date.  The September '95 DWP  (which is also out of
date) states:

  3.7.3.1  Allocation functions           [basic.stc.dynamic.allocation]

2 Each [...] allocation shall 
  yield a pointer to storage (_intro.memory_) disjoint  from  any  other
  currently allocated storage.  The pointer returned points to the start
  (lowest byte address) of the allocated storage.  If the  size  of  the
  space requested is zero, the value returned shall be nonzero and shall
  not point to or within any other  currently  allocated  storage.   The
  results of dereferencing a pointer returned as a request for zero size
  are undefined.2)

  _________________________
  2) The intent is to have operator new() implementable by calling  malยญ
  loc()  or calloc(), so the rules are substantially the same.  C++ difยญ
  fers from C in requiring a zero request to return a non-null  pointer.

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. Is this a legitimate interpretation of section 5.3.3 or is
> this a compiler/library bug?

> Incidentally, the standard template library included with gcc 2.7.2
> relies on "operator new(0)" working.
-- 
Alexandre Oliva
oliva@dcc.unicamp.br
Universidade Estadual de Campinas, S~ao Paulo, Brasil


[ 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
]
Re: Semantics of "new foo[0]"
#3538
Author: Steve Clamage
Date: Wed, 28 Feb 1996 16:16
49 lines
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
]
Re: Semantics of "new foo[0]"
#3541
Author: Dietmar Kuehl
Date: Wed, 28 Feb 1996 18:07
92 lines
4517 bytes
Hi,
G. Mike Butler D054 (mgb@kronecker.mitre.org) wrote:
: 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];

According to the DWP, 'new foo[x]' calls 'operator new[](y)' to
allocate the memory (see expr.new section 10 of the DWP) instead of
'operator new(y)'. This is a change over the ARM. Thus, I use 'operator
new[]()' in the discussion below.

Note, that this MIGHT call 'operator new[](0)' but might also call
'operator new[]()' with some different argument: The implementation is
allowed to store additional information, e.g. the number of objects
stored in the memory. If you want the check the values returned by
'operator new[]()' you have to call it directly, e.g.:

	void *ptr = operator new[](0);

... and release the memory allocated this way with 'operator delete[]()':

	operator delete[](ptr);

This mechanism is used to allocate "raw" (i.e. uninitialized) memory.
If you call 'new foo[x]' the compiler will invoke 'operator new[](y)'
according to the DWP. But it performs additional actions: It calls the
default ctor of 'foo' 'x' times to initialize the 'x' elements and
somehow remembers the 'x' to be used for later destruction when calling
'delete[] ...' (if it is necessary to remember the 'x').

:       cout << (void *)p << " " << (void *)q << endl;
:     }

: I get
:     0x338d8 0x338e8

: While these are distinct pointers, the pointers refer to overlapping
: objects. Is this a legitimate interpretation of section 5.3.3 or is
: this a compiler/library bug?

The problem is that you are mixing 'new foo[0]' and 'operator
new[](0)'.  The objects referred to in the description of 'operator
new()' in the ARM are of an arbitary type, e.g. 'char'.  Consider the
following fragment:

  void *ptr1 = operator new[](0);
  void *ptr2 = operator new[](0);

'ptr1' and 'ptr2' can indicate two adjacent bytes e.g 'ptr1 == 0x338d8'
and 'ptr2 == 0x338d9' (although I guess that it is unlikely that they
will do this). The requirement you cited rules out that 'ptr1' ==
'ptr2'.  I'm not sure whether this still applies to the rule found in
the DWP (It is not allowed to return a '0' pointer or to return a
pointer to or within currently allocated storage but it seems not to be
required to allocate any storage...).  The quote from the ARM does not
mean that 'new foo[0]' allocates enough storage to hold a 'foo' (this
was your interpretation of 'object').  It only means that two calls to
'new foo[0]' return two distinct pointers (without an intervening call
'delete'ing the object allocated with the first call to 'new foo[0]',
of course).

BTW, it results in undefined behavior to dereference the pointer
returned from a call to 'operator new(0)' or 'operator new[](0)'. The
exact rules an be found in section basic.stc.dynamic.allocation of the
DWP.

: Incidentally, the standard template library included with gcc 2.7.2
: relies on "operator new(0)" working.

I don't see where your example contradicts what is found in the ARM
or in the DWP.
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl
I am a realistic optimist - that's why I appear to be slightly pessimistic


[ 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
]
Re: Semantics of "new foo[0]"
#3551
Author: Fergus Henderson
Date: Thu, 29 Feb 1996 16:00
45 lines
2301 bytes
clamage@Eng.sun.com (Steve Clamage) writes:

>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:
[...]
>>      foo *p = new foo[0];
>>      foo *q = new foo[0];
[...]
>>I get
>>    0x338d8 0x338e8
>
>>While these are distinct pointers, the pointers refer to overlapping
>>objects.
>
>No, they don't.

You're right, but there *is* a contraction in the draft standard.

5.3.4[expr.new]/8 says "... the allocation function is called to allocate
an array with no elements", but 8.3.4[dcl.array]/1 says "An object of
array type contains a contiguously allocated non-empty set of N sub-objects".

I think that the adjective "non-empty" should be deleted from 8.3.4/8;
it already states in 8.3.4/1 that array bounds in array type declarations
must be greater than zero.

--
Any mail to me between Feb 24th and 27th may have gone to the bit bucket
(#&$^@$!# mail software silently ignored disk full error).  Please resend.
(comp.std.c++ articles were not affected, so no need to resend those.)
Fergus Henderson             	WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au              	PGP: finger fjh@128.250.37.3


[ 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
]
Re: Semantics of "new foo[0]"
#3576
Author: Steve Nutt
Date: Mon, 04 Mar 1996 18:25
50 lines
2434 bytes
mgb@kronecker.mitre.org (G. Mike Butler D054) wrote:

>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. Is this a legitimate interpretation of section 5.3.3 or is
>this a compiler/library bug?

>Incidentally, the standard template library included with gcc 2.7.2
>relies on "operator new(0)" working.

>Thanks,
>-Mike Butler
>---
>[ 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 
>]

Why do you say these are overlapping objects? You have not allocated
any objects of type foo. I would say the operator new foo[0] is
working as expected for you.

Steve
---
[ 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 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads