Topic: Operator new and 0 bytes requests


Author: private@private.com (Garrett Kajmowicz)
Date: Fri, 19 Aug 2005 16:11:15 GMT
Raw View
Are there any conditions under which operator new() can be called to
allocate 0 bytes of memory.  In addition, what action should the library
take at such time.


-      Garrett Kajmowicz

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





Author: "rich_sposato" <rds@richsposato.com>
Date: Fri, 19 Aug 2005 12:22:04 CST
Raw View
Yes, the new operator can be called to create a single empty object.

class EmptyThing
{
public:
   // all functions are non-virtual, and has no data members.
};
EmptyThing * p1 = new EmptyThing;


Some library implementations check for a size request of zero bytes,
and change it to a size request of 1 byte.

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





Author: ben-public-nospam@decadentplace.org.uk (Ben Hutchings)
Date: Sat, 20 Aug 2005 05:21:41 GMT
Raw View
Garrett Kajmowicz <private@private.com> wrote:
> Are there any conditions under which operator new() can be called to
> allocate 0 bytes of memory.

Not by the new operator.  However, the new[] operator may call
operator new[] with a size of 0, since it is legal to specify an array
size of 0 to new[] and if the element type is built-in or has a
trivial destructor the unspecified overhead required by the
implementation could also be 0.

> In addition, what action should the library take at such time.

In general I think it would be correct to treat it the same as a
request for 1 byte.

--
Ben Hutchings
It's easier to fight for one's principles than to live up to them.

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





Author: ben-public-nospam@decadentplace.org.uk (Ben Hutchings)
Date: Sat, 20 Aug 2005 05:22:43 GMT
Raw View
rich_sposato <rds@richsposato.com> wrote:
> Yes, the new operator can be called to create a single empty object.
>
> class EmptyThing
> {
> public:
>    // all functions are non-virtual, and has no data members.
> };
<snip>

This must still have a non-zero size as a complete object, even though
it can have zero size as a base sub-object.

--
Ben Hutchings
It's easier to fight for one's principles than to live up to them.

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





Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Sat, 20 Aug 2005 05:22:23 GMT
Raw View
rich_sposato wrote:
> Yes, the new operator can be called to create a single empty object.
>
> class EmptyThing
> {
> public:
>    // all functions are non-virtual, and has no data members.
> };
> EmptyThing * p1 = new EmptyThing;

Just wanted to mention that 'sizeof(EmtpyThing)' is *NOT* 0.  Thus
a request to create an instance of 'EmptyThing' is *NOT* a request
to allocate 0 bytes.

> [..]

V

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





Author: kuyper@wizard.net
Date: Sat, 20 Aug 2005 00:24:25 CST
Raw View
Garrett Kajmowicz wrote:
> Are there any conditions under which operator new() can be called to
> allocate 0 bytes of memory.  In addition, what action should the library
> take at such time.

3.7.3.1p2: "If the size of the space requested is zero, the value
returned shall not be a null pointer value (4.10). The results of
dereferencing a pointer returned as a request for zero size are
undefined."

5.3.5p7: "When the value of the _expression_ in a
_direct-new-declarator_ is zero, the allocation function is call to
allocate an array with no elements. 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.]"

This implies that a non-library operator new() can return the same
pointer for each zero-sized allocation. However, I wouldn't recommend
it.

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





Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Sun, 21 Aug 2005 23:20:32 GMT
Raw View
kuyper@wizard.net wrote:
>
> This implies that a non-library operator new() can return the same
> pointer for each zero-sized allocation. However, I wouldn't recommend
> it.

That is incorrect. In 3.7.3.1/2 it is clearly stated that: "If the
request succeeds, the value returned shall be a nonnull pointer value
(4.10) p0 different from any previously returned value p1, unless that
value p1 was subsequently passed to an operator delete."

Alberto

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





Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Mon, 22 Aug 2005 00:24:43 GMT
Raw View
Garrett Kajmowicz wrote:
> Are there any conditions under which operator new() can be called to
> allocate 0 bytes of memory.  In addition, what action should the library
> take at such time.
>

The question is a bit ambiguous, IMHO. With "calling operator new()" you
might mean:

1) calling an allocation function (as defined in 3.7.3.1)

2) calling an allocation function (as defined in 3.7.3.1) as part of the
evaluation of a new-expression (as described in 5.3.4)

With interpretation 1) all answers you seek are in 3.7.3.1/2:

"[...] Even if the size of the space requested is zero, the request can
fail. If the request succeeds, the value returned shall be a nonnull
pointer value (4.10) p0 different from any previously returned value p1,
unless that value p1 was subsequently passed to an operator delete. The
effect of dereferencing a pointer returned as a request for zero size is
undefined."

With interpretation 2) the answer depends on the kind of new-expression:

* for a non-array new-expression: the allocation is always requested to
allocate at least one byte, because no C++ object has size equal to zero
(1.8/5)

* for an array new-expression (new[]): even if called to allocate an
array of zero elements, the allocation function could still be requested
to allocate at least one byte, because of the presence of the array
allocation overhead. The size of such overhead is unspecified, and might
even be zero, though (see 5.3.4/12).

HTH,

Alberto

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





Author: "Noel Belcourt" <kbelco@sandia.gov>
Date: 22 Aug 2005 19:30:01 GMT
Raw View
Alberto Barbati wrote:
> Garrett Kajmowicz wrote:
> > Are there any conditions under which operator new() can be called to
> > allocate 0 bytes of memory.

[snip]

Along these lines I have a question as well,

Are there any types that sizeof(T) returns zero for?

Noel Belcourt

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





Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 22 Aug 2005 20:03:36 GMT
Raw View
Noel Belcourt wrote:
> Are there any types that sizeof(T) returns zero for?

No.

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





Author: kanze@gabi-soft.fr
Date: 24 Aug 2005 05:40:19 GMT
Raw View
Alberto Barbati wrote:
> kuyper@wizard.net wrote:

> > This implies that a non-library operator new() can return
> > the same pointer for each zero-sized allocation. However, I
> > wouldn't recommend it.

> That is incorrect. In 3.7.3.1/2 it is clearly stated that: "If
> the request succeeds, the value returned shall be a nonnull
> pointer value (4.10) p0 different from any previously returned
> value p1, unless that value p1 was subsequently passed to an
> operator delete."

That's interesting.  In the other passage quoted, it also says
that the allocator function cannot return a null pointer.  So
what should the system return if you call "std::operator new( 0,
std::nothrow )" 2^n times, where n is the number of bits in a
void*?

I think there might be a slight defect in the wording here,
although how it should be worded is difficult to say.

--
James Kanze                                           GABI Software
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


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





Author: bart@ingen.ddns.info (Bart van Ingen Schenau)
Date: Thu, 25 Aug 2005 02:43:48 GMT
Raw View
kanze@gabi-soft.fr wrote:

> Alberto Barbati wrote:
>> kuyper@wizard.net wrote:
>
>> > This implies that a non-library operator new() can return
>> > the same pointer for each zero-sized allocation. However, I
>> > wouldn't recommend it.
>
>> That is incorrect. In 3.7.3.1/2 it is clearly stated that: "If
>> the request succeeds, the value returned shall be a nonnull
>> pointer value (4.10) p0 different from any previously returned
>> value p1, unless that value p1 was subsequently passed to an
>> operator delete."
>
> That's interesting.  In the other passage quoted, it also says
> that the allocator function cannot return a null pointer.  So
> what should the system return if you call "std::operator new( 0,
> std::nothrow )" 2^n times, where n is the number of bits in a
> void*?
>
> I think there might be a slight defect in the wording here,
> although how it should be worded is difficult to say.

I think this defect has already been taken care of.
In my copy of the standard (the book version, which includes Technical
Corrigendum #1), there is no requirement that operator new() always
returns a non-NULL pointer.

The requirements now read (paraphrased): If the allocation succeeds, the
allocation function shall return a pointer that is distinct from all
other live pointers. An allocation request may always fail.

>
> --
> James Kanze                                           GABI Software

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

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