Memory allocation in D

Sean Kelly sean at f4.ca
Wed Dec 19 12:51:22 PST 2007


James Dennett wrote:
> Sean Kelly wrote:
>> Sean Kelly wrote:
>>> Jason House wrote:
>>>> If object sizes are a power of 2, is that because of special padding
>>>> of a
>>>> struct/object to make it align well?  Maybe some clever trick could
>>>> be done
>>>> to use wasted space, if it exists, for special tracking data...
>>>> Otherwise,
>>>> allocating a bit more seems the most sensible.
>>> Only arrays get the +1 byte added to their size during allocations. 
>>> If an object occupies 16 bytes then it will end up in a 16 byte
>>> bucket. The +1 issue for arrays exists to make the program a bit more
>>> forgiving for "past the end" errors.  Interestingly, the ANSI C
>>> standard actually requires this behavior in its allocators.
>> So I went and looked up the clause and I'm not clear whether it requires
>> the location to be physically available or merely fore the address to be
>> valid for pointer mathematics. 
> 
> C and C++ only require that the address be valid for pointer
> arithmetic; it need not be dereferencable.

Thanks.  This was what I inferred, but I had heard (or misheard) that it 
was the stronger guarantee and got confused when I didn't see this in 
the spec.  Unfortunately, as others have said, the "past the end" issue 
can cause other problems with garbage collection, so it seems that in D 
the extra byte of free space should typically be reserved anyway.


Sean



More information about the Digitalmars-d mailing list