Boost logo

Boost :

Subject: Re: [boost] How to get the size of an C-array?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2009-10-15 17:17:54

Andrey Semashev a écrit :
> 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.

No, but there is for new[] and delete[] expressions.
Indeed, delete[] needs to call the destructor on each element, and for
that it needs to know how many elements were constructed.

If the destructor is trivial, it doesn't need to call them, so there is
no need to store how much elements there are.

Note that the number of elements constructed with new[] is separate in
the general case from the number of elements allocated, since allocators
tend to work with blocks larger than needed.

Boost list run by bdawes at, gregod at, cpdaniel at, john at