Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!think.com!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.lang.lisp Subject: Re: Problems Copying Structures Keywords: defstruct structures death copying Message-ID: <1990Dec8.180825.21702@Think.COM> Date: 8 Dec 90 18:08:25 GMT References: <1990Dec8.071142.5318@cec1.wustl.edu> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 41 Ian Flanigan complained that when he copies a structure whose slots contain arrays, and then modifies one of the arrays, the array in the other structure is also modified. Structure copiers are only supposed to copy the structure itself, not the objects referenced by the structure. They are like COPY-LIST rather than COPY-TREE. If you want more, you have to write your own copier function that does precisely what you want. There's no standard function that's able to read the programmer's mind and determine which parts of a structure should be copied and which should just be referenced. Such a function is not too hard to write. Given something like: (defstruct my-structure (slot1 (make-array ...)) (slot2 (make-array ...)) (slot3 (make-array ...))) (defun copy-my-structure-and-contents (my-s) (let ((old1 (my-structure-slot1 my-s)) (old2 (my-structure-slot2 my-s)) (old3 (my-structure-slot3 my-s))) (make-my-structure :slot1 (make-array (array-dimensions old1) :element-type (array-element-type old1) :initial-elements old1) :slot2 (make-array (array-dimensions old2) :element-type (array-element-type old2) :initial-elements old2) :slot3 (make-array (array-dimensions old3) :element-type (array-element-type old3) :initial-elements old3)))) Note that this still only copies one level more than the automatically-generated copier. If you also want to copy the objects referenced in the arrays you'll have to do more. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar