bug : consecutive function calls and -inline

janderson askme at me.com
Sat May 24 12:47:48 PDT 2008


noob-is-noob wrote:
> janderson wrote:
>> noob-is-noob wrote:
>>>> sorry if it has been discussed.
>>>>> <snip old code>
>> It seems slightly odd that your implicitly copying T[].  I wonder if
>> there's even a defined behavior for copying of array indirectly by
>> struct.  I'd imagine that what the second version is doing is copying
>> the structure each time while the first is not.
>>
>> So:
>> < snip old code >
>> How that helps.
>> -Joel
> 
> Thank you.
> But it seems not related to array copying.
> I've tried with a more basic data type.
> 
> ==code==
> import std.stdio ;
> class A {
>   int c = 0 ;
>   A opCall(int b) { c = c + b ; writefln(c) ; return this ; }
> }
> struct B {
>   int c = 0 ;
>   B opCall(int b) { c = c + b ; writefln(c) ; return *this ; }
> }
> void main() {
>   A a = new A ;
>   B b ;
>   a(1)(2)(3) ;
>   a(0) ;
>   b(1)(2)(3) ;
>   b(0) ;
> }
> ==output=== (edited)
> -inline version:
> 1 3 6 6   <- class A
> 1 3 6 6   <- struct B
> non-inline version:
> 1 3 6 6   <- class A
> 1 3 6 1   <- struct B
> 
> sorry for my bad English.
> 
> 

Your right about that.  Length in an array is simply like a int.  That
is an array is:

struct Array(T)
{
   uint Length;
   T* t;
}


so if you put that in your struct you get:


struct MyStruct
{
   uint Length; //This
   T* t;
}

My point is that it is more bug prone when you start having different
lengths pointing to the same array.

The other thing is that when you exceed a the available memory in the
slot the array was allocated, t will change in that one place to a new
location.  It won't change in any of the other copies.

You might also try a pointer to an array.

-Joel


More information about the Digitalmars-d-learn mailing list