473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4238
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
2
by: Brook Young | last post by:
I am using vb .net and need to copy entire structures and objects, not just make new references. What is the best way to copy the values in the objects without making my own copy subroutine that...
1
by: Peter Schmitz | last post by:
Hi, I've got the following problem: I've got two structures in different namespaces that are just the same (name, members, types,...), but - as they are in different namespaces - I just cannot...
10
by: David Rasmussen | last post by:
If I have this struct S { int arr; }; and I do this: S s1,s2;
10
by: Xavier Noria | last post by:
Given two structures of the same size but different type, does C99 guarantee that pointers to them can be casted one to each other, and that the order of the elements will be kind of respected? ...
21
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test...
1
by: --Fragman-- | last post by:
Hello all, I'm creating some kind of object oriented drawing program, my classes consist of points, lines, quads, etc... Right now I'm implementing undo/redo and copy/paste functionality. For...
2
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.