C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defect Report: Placement array new cannot be used portably

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Ben Hutchings
Guest





PostPosted: Tue Sep 14, 2004 10:21 pm    Post subject: Defect Report: Placement array new cannot be used portably Reply with quote




[ Note: Forwarded to C++ Committee -sdc ]

An implementation may have an unspecified amount of array allocation
overhead (5.3.4/10 [expr.new]), so that evaluation of a new-expression
in which the new-type-id is T[n] involves a request for more than n *
sizeof(T) bytes of storage through the relevant operator new[]
function.

The placement operator new[] function does not and cannot check
whether the requested size is less than or equal to the size of the
provided region of memory (18.4.1.3/5-6 [lib.new.delete.placement]).
A program using placement array new must calculate what the requested
size will be in advance.

Therefore any program using placement array new must take into account
the implementation's array allocation overhead, which cannot be
obtained or calculated by any portable means.

--
Ben Hutchings
If at first you don't succeed, you're doing about average.



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

Back to top
Alberto Barbati
Guest





PostPosted: Wed Sep 15, 2004 3:04 am    Post subject: Re: Defect Report: Placement array new cannot be used portab Reply with quote



Ben Hutchings wrote:
Quote:
[ Note: Forwarded to C++ Committee -sdc ]

An implementation may have an unspecified amount of array allocation
overhead (5.3.4/10 [expr.new]), so that evaluation of a new-expression
in which the new-type-id is T[n] involves a request for more than n *
sizeof(T) bytes of storage through the relevant operator new[]
function.

The placement operator new[] function does not and cannot check
whether the requested size is less than or equal to the size of the
provided region of memory (18.4.1.3/5-6 [lib.new.delete.placement]).
A program using placement array new must calculate what the requested
size will be in advance.

Therefore any program using placement array new must take into account
the implementation's array allocation overhead, which cannot be
obtained or calculated by any portable means.


The amount of allocation overhead is added to the size parameter before
operator new[] is called (see footnote 211). Therefore the program need
to know such amount.

Maybe an example can clarify better: suppose T is a type with sizeof(T)
== 16, the overhead is 4 bytes and there are no padding or alignment
problems. Then the expression "new T[10]" would result in calling
operator new[] with a size parameter 164 (or more). The fact that in the
same situation another implementation may ask for a different amount of
memory does not make the program non-portable.

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 ]


Back to top
Ben Hutchings
Guest





PostPosted: Thu Sep 16, 2004 3:09 pm    Post subject: Re: Defect Report: Placement array new cannot be used portab Reply with quote



Alberto Barbati wrote:
<snip>
Quote:
The amount of allocation overhead is added to the size parameter before
operator new[] is called (see footnote 211). Therefore the program need
to know such amount.

Maybe an example can clarify better: suppose T is a type with sizeof(T)
== 16, the overhead is 4 bytes and there are no padding or alignment
problems. Then the expression "new T[10]" would result in calling
operator new[] with a size parameter 164 (or more). The fact that in the
same situation another implementation may ask for a different amount of
memory does not make the program non-portable.

I understand all this. You seem to have missed the word "placement".
Placement new ignores the size parameter; the size must be calculated
by the program beforehand.

--
Ben Hutchings
Quantity is no substitute for quality, but it's the only one we've got.

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


Back to top
Francis Glassborow
Guest





PostPosted: Thu Sep 16, 2004 3:11 pm    Post subject: Re: Defect Report: Placement array new cannot be used portab Reply with quote

In article <LXK1d.3554$zF3.88139 (AT) twister2 (DOT) libero.it>, Alberto Barbati
<AlbertoBarbati (AT) libero (DOT) it> writes
Quote:
The amount of allocation overhead is added to the size parameter before
operator new[] is called (see footnote 211). Therefore the program need
to know such amount.

Maybe an example can clarify better: suppose T is a type with sizeof(T)
== 16, the overhead is 4 bytes and there are no padding or alignment
problems. Then the expression "new T[10]" would result in calling
operator new[] with a size parameter 164 (or more). The fact that in
the same situation another implementation may ask for a different
amount of memory does not make the program non-portable.

Yes, but that also has a problem because one of the reasons for
providing a class version of operator new is to allow allocation from a
special memory pool. The fact that the overhead for an array is unknown
makes it significantly harder to use a special pool for operator new[].


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

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


Back to top
Alberto Barbati
Guest





PostPosted: Thu Sep 16, 2004 4:38 pm    Post subject: Re: Defect Report: Placement array new cannot be used portab Reply with quote

Ben Hutchings wrote:
Quote:

I understand all this. You seem to have missed the word "placement".
Placement new ignores the size parameter; the size must be calculated
by the program beforehand.


You're right, I missed the word "placement". I apologize for the noise.

I agree with you that, as it is defined, placement new[] is practically
useless. The worst problem is that according to 5.3.4/12 the amount of
the overhead may vary from one invocation to another. The first option I
see is to provide a mean to obtain at least an upper bound for the size
of such overhead. This could be accomplished by adding a library
function template like:

namespace std
{
template <class T>
size_t array_new_overhead(size_t n);
}

such that if the expression new T[n] results in the call

operator new[](sizeof(T) * n + y)

it's always true that y <= array_new_overhead
I don't think it should be too difficult for compiler implementors to
provide such a template. In fact, I expect most implementations to
return a costant value regardless of T and n Wink but the definition
deliberately allows a greater flexibility avoiding any change in 5.3.4.

Maybe we should turn this into a proposal?

Regards,

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 ]


Back to top
Kalle Olavi Niemitalo
Guest





PostPosted: Thu Sep 16, 2004 10:19 pm    Post subject: Re: Defect Report: Placement array new cannot be used portab Reply with quote

[email]AlbertoBarbati (AT) libero (DOT) it[/email] (Alberto Barbati) writes:

Quote:
Maybe an example can clarify better: suppose T is a type with
sizeof(T) == 16, the overhead is 4 bytes and there are no padding or
alignment problems. Then the expression "new T[10]" would result in
calling operator new[] with a size parameter 164 (or more).

The problem is with placement new[], where the memory may already
have been allocated before operator new[] is called.

#include <new>

struct T {
char data[16];
};

/* conservative guess */
const std::size_t nonportable_array_overhead = 4 * sizeof (void *);

int main()
{
char buffer[10 * sizeof (T) + nonportable_array_overhead];
new(buffer) T[10];
}

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


Back to top
Pavel Vozenilek
Guest





PostPosted: Thu Sep 16, 2004 10:19 pm    Post subject: Re: Defect Report: Placement array new cannot be used portab Reply with quote


"Ben Hutchings" <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote:

Quote:
An implementation may have an unspecified amount of array allocation
overhead (5.3.4/10 [expr.new]), so that evaluation of a new-expression
in which the new-type-id is T[n] involves a request for more than n *
sizeof(T) bytes of storage through the relevant operator new[]
function.

Yes. It was also pointed by Mirek Fiedler in:

http://groups.google.com/groups?c2coff=1&threadm=zgNx9.738%24P14.86551%40newsfep2-win.server.ntli.net
and
http://groups.google.com/groups?c2coff=1&threadm=863cqemr77.fsf%40Zorthluthik.foo

He also cites situation where it caused problem.

/Pavel



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


Back to top
Alberto Barbati
Guest





PostPosted: Thu Sep 16, 2004 11:09 pm    Post subject: Re: Defect Report: Placement array new cannot be used portab Reply with quote

Francis Glassborow wrote:

Quote:

Yes, but that also has a problem because one of the reasons for
providing a class version of operator new is to allow allocation from a
special memory pool. The fact that the overhead for an array is unknown
makes it significantly harder to use a special pool for operator new[].


I know. Overriding operator new[] is not meant for that. If you want to
use a special memory pool of fixed size objects, you'd better off with a
custom allocator. That doesn't mean that operator new[] isn't useful.
What the OP wanted to stress is that the placement operator new[]
*provided by the implementation* cannot be portably called. Ironic,
isn't it?

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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.