Testing array ptr for offset 0...

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 26 04:47:13 PDT 2016


On 05/26/2016 09:51 AM, Era Scarecrow wrote:
>   So I'm experimenting and want to override how arrays are managed for a
> short duration, this is for optimization of not having to make another
> array to keep track of lengths and then shorten them when i can in fact
> just manage it myself *very* briefly.
>
>   So, I need to make sure the structure is compatible with the built-in
> array structure (since the remainder of the calls are via normal
> convention and no allocation/resizing is done except by progressively
> smaller slices, so it should be compatible as long as it's a valid
> pointer).

I don't follow. Why can't you use a built-in array? What can you do with 
the stand-in that you can't do with the array itself?

[...]
> template isArrayPtrOffsetZero() {
>    auto check() {
>      static assert([].sizeof == (size_t.sizeof*2));
>      size_t[] arr = new size_t[1];      //length == 1.
>      return *(cast(size_t*) &arr) != 1;
>    }
>    enum isArrayPtrOffsetZero = check();
> }
[...]
>   Except this blows up and always gives the wrong answer... ==1 length
> test reverses it but is now the wrong test, and changing it to
> isArrayLengthOffsetZero will in turn give the wrong answer again...

Here's how I understand that code:
isArrayPtrOffsetZero is supposed to check if the offset of ptr is 0. You 
do that by setting the length of some array to 1, and then checking if 
the first half of the array structure is not 1. If it's 1, then length's 
offset is 0, so ptr's offset can't be 0. If it's not 1, then length's 
offset isn't 0, so ptr's offset must be 0.

Seems a bit back and forth, but as far as I can see it works as you 
want. Why do you think the answer it gives is wrong? What answer does it 
give you?


More information about the Digitalmars-d-learn mailing list