string sharing (possible bug?)

Adam P. Jenkins ajenkins at javanet.com
Wed Dec 9 15:28:47 UTC 1998


Hi, I'm pretty new to Smalltalk, so this might just be something I
don't know about.  I'm using Squeak 2.2 on Linux.

If I execute the following in the workspace, 

  |a b| 
  a := 'hello'. 
  b := 'hello'. 
  a at: 2 put: $i. 
  b

I find that b has been changed to 'hilo', even though I've modified
a.  The same behaviour occurs if I use constant arrays, i.e. every
object that I assign #(1 2 3) to points to the same object, and if I
modify one of them, the others change too.

I realize that in Smalltalk variables *point* to objects.  But in the
above code, I assigned the constant string 'hello' to "a" and "b"
separately -- I didn't assign a to b -- yet they still obviously point
to the same object.  Is this what is supposed to happen, or is this
some copy-on-write string sharing scheme gone awry?  If this is the
correct behaviour, then I suppose a good Smalltalk programming rule
would be, "don't modify an array or string object that has been
initialized from a constant", because otherwise you may inadvertantly
affect some other part of your program which also has a variable
initialized from an identical constant.  But that just doesn't seem
right to me.  I know that symbols are all unique, but I didn't know
that string and array constants are also unique.  

Thank you,
Adam





More information about the Squeak-dev mailing list