On 04/03/2011 05:06 PM, Jonathan M Davis wrote:
On 2011-04-03 04:10, simendsjo wrote:
        int[] a = [1,2,3];

        int[4] b;
        assert(b == [0,0,0,0]);
        b = a[] * 3; // oops... a[] * 3 takes element outside a's bounds
        assert(b[$-1] == 0); // fails.. last element is *(a.ptr+3) * 3

Array bounds checking is done on code which is not compiled with the -
noboundscheck flag and which is either not built with -release or is @safe.

I assume that you're not compiling with -noboundscheck (which turns off all
array bounds checking). So, you're likely compiling with -release on code
which isn't @safe. @system is the default, so unless you've marked your code
@safe or you're not compiling with -release, I wouldn't expect there to be any
bounds checking. If you want to guarantee that there's always bounds checking,
then you need to mark your code @safe and not use -noboundscheck. However,
given how little of Phobos is currently @safe or @trusted, odds are that
trying to mark your code @safe will get _really_ annoying at this point. And
to fix that, we'd likely need conditional @safe and conditional @trusted for
the same reasons that we need conditional pure. And those haven't been taken
care of yet (there isn't even an official plan to as far as I know - though
hopefully there will be).

- Jonathan M Davis

This is the really verbose answer to the question I thought you were asking as well.

I think this problem is better expressed like this:


import std.stdio;
void main()
{
    int[] a = [1,2,3];
    int[4] b;
    int[4] c = [0,1,2,3];
    int[5] d = [0,1,2,3,4];
    assert(b == [0,0,0,0]);
    b = c[] * 3; // like foreach(val; c) b.append(val*3);
    writef("%s %s %s\n", a, b, c);
    b = a[] * 3; // No error
    writef("%s %s %s\n", a, b, c);
    b = a[]; // object.Exception: lengths don't match for array copy
    writef("%s %s %s\n", a, b, c);
writef("%s\n", a[] * 3); // bounds.d(15): Error: Array operation a[] * 3 not implemented
    assert(b[$-1] == 0);
}

I think bearophile addressed this, but I can't quite tell.

So now I'm curious, why does the multiply operation break the bounds check? Also, why does it fail to print? The result can be stored in another array, so I would think it would print.

Reply via email to