BigInt -- a challenge for std.allocator

Don x at nospam.com
Tue Oct 29 05:22:54 PDT 2013


On Tuesday, 29 October 2013 at 11:26:45 UTC, Froglegs wrote:
>
>>    BigInt n = 100;
>>    n += 10;
>>    ++n;
>>
>> ... and both the 2nd and 3rd lines will result in a 
>> reallocation even though there is no need for it, because only 
>> n is referencing the memory it wraps.
>
>
>  Does BigInt not have overloaded operators for adding normal 
> integers? Is it converting 10 to a BigInt prior to adding it to 
> n?

Yes, it has overloaded operators, but that's not relevant. The 
issue is that
initially n points to an block of memory, containing [100].

Then, if you use COW, n+= 10 isn't in-place. It has to create a 
new array, containing [110].
Then ++n creates a third array. [111].



More information about the Digitalmars-d mailing list