Manually-allocated memory and maximum array capacity

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Tue Apr 8 11:40:07 PDT 2014


On 08/04/14 16:58, Artur Skawina wrote:
> Just be careful; I used the name 'capacity' because it fit
> into your example, but 'capacity' shouldn't be overloaded like
> that - it works very differently from the magic built-in property.

Yes, I was thinking that I'd better use a different name.  (Actually the precise 
requirements are a bit different anyway, so there's no way to use it quite like 
that; but the principle is right.)

> The only safe way to use it would be:
>
>     E[] aalloc(E)(size_t l) @trusted {
>        if (l<size_t.max/E.sizeof)
>           if (auto p = cast(E*)malloc(l*E.sizeof))
>              return p[0..malloc_usable_size(p)/E.sizeof];
>        return null;
>     }

Nicely done! :-)



More information about the Digitalmars-d-learn mailing list