From bug-octave-request at bevo dot che dot wisc dot edu Wed Sep 18 02:22:54 2002 Subject: Bug in octave structures From: "John W. Eaton" To: Roderick Koehle Cc: bug-octave at bevo dot che dot wisc dot edu, octave-maintainers@bevo.che.wisc.edu Date: Wed, 18 Sep 2002 02:22:01 -0500 On 17-Sep-2002, Roderick Koehle wrote: | In the current octave CVS release, I discovered following problems: | | octave:1> z = zeros(1,5); | octave:2> s.a = z; s.b = z; s.a(1) = 1; s | s = | { | a = | | 1 0 0 0 0 | | b = | | 1 0 0 0 0 | | } | It seems that by assigning the vector z to s.a and s.b, these variables stay | linked forever. Whatever you write into the vector s.a will also modify s.b. | | Doing the same with scalar values however works properly. So the example: | | octave:1> z = 0; s.a = z; s.b = z; s.a(1) = 1; s | s = | { | a = 1 | b = 0 | } | | So for the scalar case, above example produces the correct result, writing | into a does not cause b to be modified. | | The following example produces also curious results. | A function "empty" returns a structure with an empty list element. | When you write something into this list and then try to clear it again with | the function "empty", you will get following: | | octave:1> function a=empty(), a.list = {}; endfunction | octave:2> a=empty() | a = | { | list = {} | } | | octave:3> a.list{1} = 'hallo' | a.list = | { | list = | { | [1,1] = hallo | } | } | | octave:4> a = empty() | a = | { | list = | { | [1,1] = hallo | } | } | | Even though in the function "empty" the empty list is assigned to a.list. It | won't change its content. Please try the following patch. It's checked in to CVS now too, so you could also update to get these changes. Thanks, jwe 2002-09-18 John W. Eaton * ov-struct.cc (octave_struct::subsasgn): Ensure that indexed object is not shared before calling subsasgn. Index: ov-struct.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/ov-struct.cc,v retrieving revision 1.22 diff -u -r1.22 ov-struct.cc --- ov-struct.cc 3 Jun 2002 18:15:47 -0000 1.22 +++ ov-struct.cc 18 Sep 2002 07:19:16 -0000 at @ -239,6 +239,8 @@ next_idx.remove_front (); next_idx.remove_front (); + u.make_unique (); + t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); } } at @ -274,6 +276,8 @@ SLList next_idx (idx); next_idx.remove_front (); + + u.make_unique (); t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); } ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------