[ale] Copy a Structure - A C Question

Fletch fletch at phydeaux.org
Fri Jun 14 10:48:47 EDT 2002


>>>>> "Terry" == Terry Lee Tucker <terry at esc1.com> writes:

    Terry> Thanks for the response. All the strings in the structure
    Terry> are declared as character pointers and allocated
    Terry> dynamically. Yet, aparently, both memcpy and bcopy work
    Terry> corrctly. Am I setting myself up for some kind of strange
    Terry> core dump later on?

        Possibly.  As was pointed out, assigning structure pointers
will do a shallow copy (what I was proposing was a deep copy).  The
pointers themselves will be copied, but they'll still point to the
same blocks of memory.  This leaves you open to aliasing problems,
where you modify one copy but it affects all copies.  If you free one
string, you may core dump if you try and access it from another copy
of the struct.


        Not that there's anything wrong with just doing a shallow
copy, or anything better about a deep copy.  You just need to be
cognizant of the issues with using each.


-- 
Fletch                | "If you find my answers frightening,       __`'/|
fletch at phydeaux.org   |  Vincent, you should cease askin'          \ o.O'
770 933-0600 x211(w)  |  scary questions." -- Jules                =(___)=
770 294-0820 (m)      |                                               U

---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list