array copy/ref question

Koroskin Denis 2korden at gmail.com
Tue Aug 12 07:45:09 PDT 2008


On Tue, 12 Aug 2008 18:12:52 +0400, Jesse Phillips  
<jessekphillips at gmail.com> wrote:

> On Tue, 12 Aug 2008 02:42:17 +0000, Jesse Phillips wrote:
>
>> main( string[] args )
>> {
>>     int[] test = [ 1, 2, 3, 4 ];
>>     int[] copy = new int[4];
>>
>>     copy[]  = test;
>>     test[1] = 11;
>>
>>     assert( test[1] == 11 );
>>     assert( copy[1] == 2 );
>>
>>     writefln( "Copy: test[1] %d copy[1] %d", test[1], copy[1] );
>> }
>
> I know I created that first one to be smaller, here is what it was
> supposed to be.
>
> void main( string[] args )
> {
>     int[] test = [ 1, 2, 3, 4 ];
>     int[] copy;
>
>     copy = test;
>
>     copy[]  = test;
> }

It can be rewritten as following:

void main()
{
     int[] test = [ 1, 2, 3, 4 ];
     test[] = test;
}

Do you see now that arrays overlap?
It is illegal, use custom routines if you suspect that your data overlaps.



More information about the Digitalmars-d mailing list