www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Variable assignment

reply okibi <okibi ratedo.com> writes:
I've been writing an xml editor and testing some code. I ran into this problem
with 1.006+ (didn't have the problem in 1.00, jumped straight to 1.006 after
that).

The problem is declaring variables in a class, and then setting them within a
function inside the class. For example, I declare both var1 and var2. I use an
open function and set var1 to the file selected. Then I run an export function
and set var2 to that file. When I issue the save function afterwords, var1 was
adjusted to the same value as var2. I found out that this happens while setting
var2.

Any tips or do you need more info?
Mar 26 2007
next sibling parent Johan Granberg <lijat.meREM OVE.gmail.com> writes:
okibi wrote:

 I've been writing an xml editor and testing some code. I ran into this
 problem with 1.006+ (didn't have the problem in 1.00, jumped straight to
 1.006 after that).
 
 The problem is declaring variables in a class, and then setting them
 within a function inside the class. For example, I declare both var1 and
 var2. I use an open function and set var1 to the file selected. Then I run
 an export function and set var2 to that file. When I issue the save
 function afterwords, var1 was adjusted to the same value as var2. I found
 out that this happens while setting var2.
 
 Any tips or do you need more info?
If you can show us the source it would probably help.
Mar 26 2007
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 26 Mar 2007 13:19:13 -0400, okibi wrote:

 I've been writing an xml editor and testing some code. I ran into this problem
with 1.006+ (didn't have the problem in 1.00, jumped straight to 1.006 after
that).
 
 The problem is declaring variables in a class, and then setting them within a
 function inside the class. For example, I declare both var1 and var2. I use
 an open function and set var1 to the file selected. Then I run an export
 function and set var2 to that file. When I issue the save function afterwords,
 var1 was adjusted to the same value as var2. I found out that this happens
 while setting var2.
 
 Any tips or do you need more info?
This sounds like you are doing this, in effect. char[] var1; char[] var2; var1 = filename; . . . filename = something; . . . var2 = filename; . . . // Now var1 == var2; If so, then remember that assigning to an array only makes both arrays point to the same data in RAM. If you later change that source array in a way that doesn't make it allocate new RAM, you still have both vars pointing to the same RAM. Try ".dup" if you really need to assign data from an array rather than copy the reference. char[] var1; char[] var2; var1 = filename.dup; . . . filename = something.dup; . . . var2 = filename.dup; . . . // Now var1 != var2; -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Mar 26 2007
parent reply okibi <okibi ratedo.com> writes:
That was exactly my problem! Now, why did it work with dmd 1.00 and not 1.006+?
Did the method change within that time? I don't remember having to do this
before.

Derek Parnell Wrote:

 This sounds like you are doing this, in effect.
 
   char[] var1;
   char[] var2;
 
   var1 = filename;
   . . .
   filename = something;
   . . .
   var2 = filename;
   . . .
   // Now var1 == var2;
 
 If so, then remember that assigning to an array only makes both arrays
 point to the same data in RAM. If you later change that source array in a
 way that doesn't make it allocate new RAM, you still have both vars
 pointing to the same RAM.
 
 Try ".dup" if you really need to assign data from an array rather than copy
 the reference.
 
   char[] var1;
   char[] var2;
 
   var1 = filename.dup;
   . . .
   filename = something.dup;
   . . .
   var2 = filename.dup;
   . . .
   // Now var1 != var2;
 
 -- 
 Derek Parnell
 Melbourne, Australia
 "Justice for David Hicks!"
 skype: derek.j.parnell
Mar 27 2007
parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 27 Mar 2007 07:11:06 -0400, okibi wrote:

 That was exactly my problem! Now, why did it work with dmd 1.00 and not
1.006+? 
 Did the method change within that time? I don't remember having to do this
before.
I suspect that the internals of the GC have changed and now it can sometimes alter an array's contents without reallocating RAM whereas earlier versions tended to allocate a new RAM block. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Mar 27 2007
parent reply okibi <okibi ratedo.com> writes:
Makes sense to me. I wasn't aware it was going to change, though. That would've
been nice to know!

Anyways, thanks for your help. I greatly appreciate it!

Derek Parnell Wrote:

 On Tue, 27 Mar 2007 07:11:06 -0400, okibi wrote:
 
 That was exactly my problem! Now, why did it work with dmd 1.00 and not
1.006+? 
 Did the method change within that time? I don't remember having to do this
before.
I suspect that the internals of the GC have changed and now it can sometimes alter an array's contents without reallocating RAM whereas earlier versions tended to allocate a new RAM block. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Mar 27 2007
parent Derek Parnell <derek psych.ward> writes:
On Tue, 27 Mar 2007 08:44:05 -0400, okibi wrote:

 Makes sense to me. I wasn't aware it was going to change, though. That
would've been nice to know!
In the change log for the latest release it says "Improved gc performance for array resize and append" and that's what I'm basing my guess on. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Mar 27 2007