compiled program error

John Nixon via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 7 13:55:33 PDT 2015


On Sunday, 7 June 2015 at 19:05:16 UTC, Adam D. Ruppe wrote:
> On Sunday, 7 June 2015 at 18:59:00 UTC, John Nixon wrote:
>> double[] pvi,int_1,int_2;
>>
>>  int_1=pvi_calc(centre);
>>  int_2=pvi_calc(n-1-centre);//pvi_calc is changing int_1!
>
>> double[] pvi_calc(const int n1){
>>  return pvi;}
>
>
> These are the relevant lines: pvi_calc writes to a global array 
> (pvi) then returns it.
>
> So the first two lines are just setting int_1 and int_2 both to 
> pvi, the same thing. So since they all reference the same 
> array, writing to any of them changes all of them.
>
> You'll probably want to move double[] pvi to inside pvi_calc so 
> it becomes a new copy, or at least `return pvi.dup;` instead of 
> just `return pvi;`

Thank you for your concise reply. I had forgotten the distinction 
between arrays and slices that is described in Ali Chehreli's 
book Programming in D.
John Nixon


More information about the Digitalmars-d mailing list