Memory allocation in D (noob question)
Sean Kelly
sean at f4.ca
Mon Dec 3 11:19:51 PST 2007
Steven Schveighoffer wrote:
> "mandel" wrote in message
>> Robert Fraser Wrote:
>>
>>> mandel wrote:
>>>> It probably is a noob question,
>>>> but aren't array lengths just hidden size_t values
>>>> that are passed around?
>>>> Why do we need to allocate space for them, too?
>>>>
>>>> voif foo()
>>>> {
>>>> size_t length;
>>>> char* ptr; //allocated memory of 2^n
>>>> //.. the same as..?
>>>> char[] data;
>>>> }
>>> The extra space allocated isn't for the length (in fact, it's just a
>>> byte I think); it's to make checking for array bounds errors possible
>>> (since there's a byte of space that, if accessed, indicates an
>>> overflow). I tmight be used for something else, too.
>> Thanks, that answers my question.
>> But I can't think how it could be used for array bounds errors checking
>> right now.
>> Well, I guess there some ng post about this, somewhere.
>> But the page allocation overhead looks ugly for a language like D.
>
> Think of it this way:
>
> int[] array1 = new int[5];
> int[] array2 = new int[5];
>
> imagine that array 1 and array 2 are now sequential in memory *AND* there is
> no extra byte separating them.
>
> Now I create the valid array slices:
>
> int[] array3 = array1[$..$];
> int[] array4 = array2[0..0];
>
> Note that both of these arrays are bit-for-bit identical (both have 0 length
> and the same ptr value). Which one points to which piece of memory? How is
> the GC to decide which memory gets collected?
>
> These are the types of problems that the extra byte helps with.
>
> I personally think there exists a way to fix this efficiently without adding
> the extra byte, but I can't think of one :)
>
> Oh, and also, the size_t length is not stored in the allocated memory. It's
> stored in the array structure, usually on the stack or inside a class
> instance.
This is true in D 1.0. However, there has been talk that arrays in D
2.0 would change from:
struct Array
{
size_t length;
byte* ptr;
}
to:
struct Array
{
byte* ptr;
byte* end;
}
Which would make every array reference always point to itself and to the
block immediately following it in memory, if no padding is done.
Sean
More information about the Digitalmars-d
mailing list