www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dynamic array length ABI unit

reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
Regarding the D ABI, http://dlang.org/abi.html , I've noticed 
that the description for dynamic arrays seems ambiguous:

     offset   property  contents
     0        .length   array dimension
     size_t   .ptr      pointer to array data

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.
Jun 16 2014
next sibling parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
""Luís Marques " wrote in message 
news:fykakpufqivskbnusxgl forum.dlang.org...
 Regarding the D ABI, http://dlang.org/abi.html , I've noticed that the 
 description for dynamic arrays seems ambiguous:

      offset   property  contents
      0        .length   array dimension
      size_t   .ptr      pointer to array data

 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.
Jun 16 2014
prev sibling next sibling parent "David Nadlinger" <code klickverbot.at> writes:
On Monday, 16 June 2014 at 11:44:53 UTC, Luís Marques wrote:
 Regarding the D ABI, http://dlang.org/abi.html , I've noticed 
 that the description for dynamic arrays seems ambiguous:

     offset   property  contents
     0        .length   array dimension
     size_t   .ptr      pointer to array data

 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.

"Dimension" seems to say "number of elements" to me without much room for interpretation (which it indeed is). Feel free to open a pull request if you have an idea for a better description, though. Best, David
Jun 16 2014
prev sibling next sibling parent =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
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]);
Jun 16 2014
prev sibling next sibling parent =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
On Monday, 16 June 2014 at 12:27:03 UTC, David Nadlinger wrote:
 "Dimension" seems to say "number of elements" to me without 
 much room for interpretation (which it indeed is).

 Feel free to open a pull request if you have an idea for a 
 better description, though.

I think you are right, my comment made more sense if it was the other way (number of bytes). My bad, sorry for decreasing the S/N ratio of the forum :P
Jun 16 2014
prev sibling parent "David Nadlinger" <code klickverbot.at> writes:
On Monday, 16 June 2014 at 13:11:24 UTC, Luís Marques wrote:
 Ah, OK. I was mislead by the following:

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

Ah, that clears up the confusion. Array casts are "smart" and automatically divide down the length as needed, or fail otherwise. David
Jun 16 2014