Topic: Asking for zero bytes


Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/27
Raw View
smeyers@aristeia.com (Scott Meyers) writes:

>In section 5.5.3 of the ARM, it's stated that when operator new is called
>with an argument of 0, a pointer to a distinct object is returned (distinct
>for each such call).  ...

> ... it seems that the standard says nothing about
>the behavior of operator new (*not* operator new[]) or any user-defined
>operator new[] when they are passed a size of zero, and the requirement
>that repeated calls to new return distinct pointers seems to have vanished.

That observation is issue #9 on the C++ committee Library issues
list. A proposed handling -- requiring distinct non-null pointer
returns if the request does not fail -- was submitted at the meeting
that just ended, but has not yet been voted upon. It should be
voted on at the next meeting in April. (No decision is final until
a Technical Corrigendum is issued, which will not happen before
the end of next year.)

I don't believe that removing the "distinct pointer" requirement
was deliberate. But the current wording requires a non-null
return from a request for zero size. Since the address space
might be exhausted, you can't require both distinct pointers and
a non-null return. (A "nothrow" version of operator new can
signal failure only via a null return value.)

It seems to me that the correct resolution is to allow an
operator new to fail if it cannot return a distinct pointer.

--
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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: smeyers@aristeia.com (Scott Meyers)
Date: 1999/10/27
Raw View
In section 5.5.3 of the ARM, it's stated that when operator new is called
with an argument of 0, a pointer to a distinct object is returned (distinct
for each such call).  The corresponding material in the standard is 5.3.4
para 7, which reads like this (I'm quoting from the FDIS, because my copy
of the standard is copy-protected):

  When  the  value of the expression in a direct-new-declarator is zero,
  the allocation function is called to allocate an array  with  no  ele-
  ments.   The  pointer  returned  by  the  new-expression  is non-null.
  [Note: If the library  allocation  function  is  called,  the  pointer
  returned is distinct from the pointer to any other object.  ]

The grammar shows that a direct-new-declarator is applicable only to
allocations for arrays, so the above paragraph applies only to arrays.  In
conjunction with the note, it seems that the standard says nothing about
the behavior of operator new (*not* operator new[]) or any user-defined
operator new[] when they are passed a size of zero, and the requirement
that repeated calls to new return distinct pointers seems to have vanished.
Is this correct?  If so, does anybody know why this change (wrt the ARM)
was made?

Thanks,

Scott

--
Scott Meyers, Ph.D.                  smeyers@aristeia.com
Software Development Consultant      http://www.aristeia.com/
Visit http://meyerscd.awl.com/ to demo the Effective C++ CD
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]