Dynamic array length ABI unit

via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 16 06:11:23 PDT 2014


On Monday, 16 June 2014 at 11:54:27 UTC, Daniel Murphy wrote:
>> Couldn't "array dimension" be either in bytes or in T units, 
>> for a T[]? It seems to be in bytes (I had assumed it was in T 
>> units). If you agree that it is ambiguous, please tweak the 
>> description to be more precise.
>
> It _is_ in T units.  It is exactly the dimension of the array, 
> not the size in memory.

Ah, OK. I was mislead by the following:

	1: int[] a1 = [42];
	2: ubyte[] a2 = cast(ubyte[]) a1;
	3: writeln(a2);

I thought line 2 did a reinterpret_cast on the a1 array header 
(length + ptr), so I expected a2 == [42], but it is equal to [42, 
0, 0, 0] (casts the array contents, adjusts the array length). 
Forcing a reinterpret_cast-style cast shows that indeed length is 
in T units, as I originally expected:

	int[] a1 = [42];
	void* p = &a1;
	ubyte[] a2 = *cast(ubyte[]*) p;
	assert(a2 == [42]);


More information about the Digitalmars-d mailing list