Boost logo

Boost :

Subject: Re: [boost] How to get the size of an C-array?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2009-10-15 13:50:59


Mathias Gaunard wrote:
> vicente.botet wrote:
>
>> Can someone in this list explain why the standard don't allows to
>> recover the number of elements allocated by new[]? What is the deep
>> reason?
>
> I suppose the reason is to not introduce a distinction between types
> with destructors and types without.
>
> If this was to be provided, a meta-function to tell whether a type has a
> trivial destructor or not would be needed as well.

Why? From the underlying allocator point of view there's not much
difference in allocating memory for PODs and non-PODs.

My guess is that this is not possible in order to allow trivial
implementation of new/delete allocators in terms of malloc/free. The
latter don't allow to acquire the size of allocated buffer, so in order
to provide this ability for new/delete it would have to add its own
header to store the size, which would lead to more extensive memory
consumption. It's a pity that malloc/free allocator don't allow buffer
size acquisition, especially since the size is most likely stored somehow.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk