Mr John FO Evans <mi***@orpheusmail.co.ukwrites:
In article <d8******************************@bt.com>, "Malcolm McLean"
<re*******@btinternet.comwrote:
>C will perform a shallow copy. The pointers will be copied, the memory
they point to won't be dupicated. If the structure cotains any pointers to
itself or own elements, things will break.
If you are implementing a C emulator you should follow ANSI by default.
However if you want to add an option in which such copies are disabled
then you might possibly improve the language. It is a moot point what the
standard should say.
Thank you both for explaining the expected response to copying structures.
Because this emulator is intended for embedding there will be pointers to
external memory which would have been contiguous in a non-embedded C
implimentation. I am therefore tempted to (try to) implement full (not
shallow) copying since this would seem to make the result more like a
conventional situation rather than less.
If a struct assignment does anything more than copying just the
contents of the structure itself, including any pointer members, but
not including anything that they point to (a shallow copy), then the C
implementation is broken. The semantics of struct assignment are well
defined.
If you're implementing something other than C, that's fine, but then
comp.lang.c is the wrong place to ask about it.
If you want to implement, as an extension, an additional construct
that does some sort of deep copy, feel free to do so. But I'm not
sure how you can do this. The real problem isn't just implementing a
deep copy operation, it's defining just what it's supposed to do in
the first place.
For example:
struct dynamic_string {
char *s;
size_t len;
};
struct dynamic_string source = { "hello", 5 };
struct dynamic_string target;
_DEEP_COPY(target, source);
source.s is a pointer to a char, but it happens to point to the first
element of a string. Does your _DEEP_COPY operation copy just the
single char that source.s points to, or does it copy the 6 characters
of the string that it points to (up to and including the terminating
'\0'), or something else?
Given something declared as a char*, the compiler has no way of
knowing whether it points to a single char, to a fixed-size array of
char, to a string terminated by '\0', or to something else entirely.
And assuming you can solve that problem, where do you copy the data
to? Do you assume that target.s already points to enough space to
hold whatever source.s points to? Or do you implicitly allocate
enough memory?
In general, if you want a deep copy operation, you need to implement
it specifically (and most likely manually) for each type. The type
declaration itself just doesn't have enough information to indicate
how to do a deep copy.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"