Allocate N elements

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jul 15 13:34:42 PDT 2013


On Mon, Jul 15, 2013 at 09:53:32PM +0200, Namespace wrote:
> On Monday, 15 July 2013 at 18:29:12 UTC, Adam D. Ruppe wrote:
> >On Monday, 15 July 2013 at 18:16:45 UTC, Namespace wrote:
> >>writeln(arr.length, "::", arr.capacity);
> >
> >arr.capacity checks the GC block, and since you malloced it, there
> >is no gc block for it to check. So it simply doesn't know if
> >there's any extra capacity there and reports 0 just to be safe.
> 
> Ah, good to know. But anyway malloc allocates exact N elements,
> without ugly overhead.
[...]

I doubt malloc has no overhead. AFAIK, many malloc implementations store
some kind of bookkeeping info in the area of memory just before the
pointer that's returned. After all, malloc/free has to somehow know
which memory blocks are allocated and which are free. Some
implementations also store canary values in that area in order to detect
memory corruptions.


T

-- 
Старый друг лучше новых двух.


More information about the Digitalmars-d-learn mailing list