Topic: Allocation of Zero Bytes


Author: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Mon, 12 Jul 2004 23:43:11 +0000 (UTC)
Raw View
""Alexey Sarytchev"" <alx_sar@hotmail.com> wrote in message
news:10eu5bfs9gvuk99@corp.supernews.com...
> ""Sergey P. Derevyago"" <non-existent@iobox.com> wrote in message
> news:40ED23D5.699BD5BC@iobox.com...
> > Greg Hickman wrote:
> > > Why does 3.7.3.1(2) require a non-null pointer value to be returned by
> an
> > > allocation function when the size of the requested space is zero?
> > >
> > 1. It must be possible to acquire a zero-length array or block of memory
> and
> > the address of this block must be a valid pointer (right to copy,
compare
> and
> > pass back to the deallocation function).
> > 2. In some situations, more simple and optimal algorithm can be used if
> these
> > pointers to zero-length memory are unique (not NULL in particular).
>
> Standard doesn't require those pointers to be unique. They just shouldn't
be
> null.
> They may be all the same or all different or anything in between.
>

Well, the standard says in 5.3.4p7:
"When the value of the expression in a directnewdeclarator is
zero, the allocation function is called to allocate an array
with no elements. The pointer returned by the newexpression
is nonnull.

[Note: If the library allocation function is called, the pointer
returned is distinct from the pointer to any other object. ]"

Which looks like an implicit requirement to me :-)

Roger Orr.
--
MVP in C++ at www.brainbench.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                       ]





Author: petebecker@acm.org (Pete Becker)
Date: Tue, 13 Jul 2004 02:50:23 +0000 (UTC)
Raw View
Roger Orr wrote:
>
> Well, the standard says in 5.3.4p7:
> "When the value of the expression in a directnewdeclarator is
> zero, the allocation function is called to allocate an array
> with no elements. The pointer returned by the newexpression
> is nonnull.
>
> [Note: If the library allocation function is called, the pointer
> returned is distinct from the pointer to any other object. ]"
>
> Which looks like an implicit requirement to me :-)
>

That part has been reworked in the current working draft to clarify the
rule. 5.3.4/9 now says:

> When the value of the expression in a direct-new-declarator is zero,
> the allocation function is called to allocate an array
> with no elements.

And then you have to track back to 3.7.3.1/2, which gives the
requirements for the allocation function, and ends with:

> If the request succeeds, the value returned
> shall be a non-null 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.

So it used to be unclear, but now it's explicit: zero-size allocations
are distinct.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.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                       ]





Author: greg.hickman@lmco.com (Greg Hickman)
Date: Thu, 8 Jul 2004 00:14:06 +0000 (UTC)
Raw View
Why does 3.7.3.1(2) require a non-null pointer value to be returned by an
allocation function when the size of the requested space is zero?

Thanks,
Greg



---
[ 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: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Thu, 8 Jul 2004 19:58:23 +0000 (UTC)
Raw View
Greg Hickman wrote:
> Why does 3.7.3.1(2) require a non-null pointer value to be returned by an
> allocation function when the size of the requested space is zero?
>
 1. It must be possible to acquire a zero-length array or block of memory and
the address of this block must be a valid pointer (right to copy, compare and
pass back to the deallocation function).
 2. In some situations, more simple and optimal algorithm can be used if these
pointers to zero-length memory are unique (not NULL in particular).

P.S. It's OK to have zero-length arrays; the error is an attempt to access
their elements.
--
         With all respect, Sergey.               http://ders.angen.net/
         mailto : ders at skeptik.net

---
[ 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: musiphil@bawi.org (Seungbeom Kim)
Date: Thu, 8 Jul 2004 21:12:17 +0000 (UTC)
Raw View
Greg Hickman wrote:

> Why does 3.7.3.1(2) require a non-null pointer value to be returned by an
> allocation function when the size of the requested space is zero?

In that way, all the return values of the allocation function
can be treated in the same way regardless of the requested size,
thus simplifying the client programs.

--
Seungbeom Kim

---
[ 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: alx_sar@hotmail.com ("Alexey Sarytchev")
Date: Sun, 11 Jul 2004 02:07:14 +0000 (UTC)
Raw View
""Sergey P. Derevyago"" <non-existent@iobox.com> wrote in message
news:40ED23D5.699BD5BC@iobox.com...
> Greg Hickman wrote:
> > Why does 3.7.3.1(2) require a non-null pointer value to be returned by
an
> > allocation function when the size of the requested space is zero?
> >
> 1. It must be possible to acquire a zero-length array or block of memory
and
> the address of this block must be a valid pointer (right to copy, compare
and
> pass back to the deallocation function).
> 2. In some situations, more simple and optimal algorithm can be used if
these
> pointers to zero-length memory are unique (not NULL in particular).

Standard doesn't require those pointers to be unique. They just shouldn't be
null.
They may be all the same or all different or anything in between.

The main reason, as I can see it, is to have null pointer as an error value
for
non-throw allocation functions. It is not an error to allocate a space of
size zero.
See 3.7.3.1/3

Regards,
Alexey Sarytchev.


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