472,101 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,101 software developers and data experts.

Copying Structures

Am looking at adding structures to an embedded C emulator but am wondering
what the standard is for copying structures(ie structa=structb) which
contain pointers to memory and which are therefore not contiguous in memory.

When such structures are copied what should C do with these elements? Or
should copying be illegal in this case?

John
--
_ _________________________________________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_/ 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinternet.co.uk
Mar 25 '07 #1
9 4151
Mr John FO Evans wrote:
Am looking at adding structures to an embedded C emulator but am
wondering what the standard is for copying structures(ie structa=structb)
which contain pointers to memory and which are therefore not contiguous
in memory.

When such structures are copied what should C do with these elements?

1. Copying structures is legal and should be supported. It behaves like a
memberwise assignment.
2. If the structure contains pointers, you copy the pointers, just as
assigning pointers does assign the pointers. If the application logic
requires further action is not up to the compiler/interpreter/emulator.
Or should copying be illegal in this case?
No.

Uli

Mar 25 '07 #2
Mr John FO Evans wrote, On 25/03/07 16:47:
Am looking at adding structures to an embedded C emulator but am wondering
<nit>don't you mean C interpreter?</nit>
what the standard is for copying structures(ie structa=structb) which
contain pointers to memory and which are therefore not contiguous in memory.

When such structures are copied what should C do with these elements? Or
should copying be illegal in this case?
You should copy the values of the pointers and not what they point to.
I.e. the simplest way to implement structure assignment would be
memcpy(&structa,&structb,sizeof structa);

As you are the implementer you should get a copy of whichever C standard
you are working towards, see
http://clc-wiki.net/wiki/C_standardisation:ISO for where you can obtain
free drafts or buy the actual thing.
--
Flash Gordon
Mar 25 '07 #3

"Mr John FO Evans" <mi***@orpheusmail.co.ukwrote in message
>
Am looking at adding structures to an embedded C emulator but am
wondering what the standard is for copying structures(ie structa=structb)
which contain pointers to memory and which are therefore not contiguous in
memory.

When such structures are copied what should C do with these elements? Or
should copying be illegal in this case?
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 25 '07 #4
On Mar 25, 8:47 pm, Mr John FO Evans <m...@orpheusmail.co.ukwrote:
Am looking at adding structures to an embedded C emulator but am wondering
what the standard is for copying structures(ie structa=structb) which
contain pointers to memory and which are therefore not contiguous in memory.

When such structures are copied what should C do with these elements? Or
should copying be illegal in this case?

John

--
_ _________________________________________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_/ 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinternet.co.uk
Be cautious about shallow copy problem.

Cheers
-Vallabha

Mar 26 '07 #5
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.

John
--
_ _________________________________________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_/ 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinternet.co.uk
Mar 26 '07 #6
Mr John FO Evans wrote:
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.
Don't go there, consider the case where a pointer is an opaque type, or
points to an object that required some form or initialisation and
shouldn't just be copied.

--
Ian Collins.
Mar 26 '07 #7
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"
Mar 26 '07 #8
Mr John FO Evans wrote:
"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.
.... snip ...
>
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.
Attempting to implement a deep copy for generic structures is
inherently impossible without knowing the detailed composition of
all those structures. This is the sort of thing that leads to C++
bloat. Besides which, it is contrary to the C standard.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #9
CBFalconer wrote:
Mr John FO Evans wrote:
>>"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.

.... snip ...
>>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.


Attempting to implement a deep copy for generic structures is
inherently impossible without knowing the detailed composition of
all those structures. This is the sort of thing that leads to C++
bloat. Besides which, it is contrary to the C standard.
In C++, just like C, you only pay for what you use.

--
Ian Collins.
Mar 27 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by kazack | last post: by
2 posts views Thread by Brook Young | last post: by
1 post views Thread by Peter Schmitz | last post: by
10 posts views Thread by David Rasmussen | last post: by
10 posts views Thread by Xavier Noria | last post: by
21 posts views Thread by hermes_917 | last post: by
1 post views Thread by --Fragman-- | last post: by
2 posts views Thread by thomasfarrow | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.