473,795 Members | 3,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4262
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,sizeo f 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***@orpheusm ail.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...@orpheusma il.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*******@btin ternet.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*******@btin ternet.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***@orpheusm ail.co.ukwrites :
In article <d8************ *************** ***@bt.com>, "Malcolm McLean"
<re*******@btin ternet.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(targ et, 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_Keit h) 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*******@btin ternet.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*******@btin ternet.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
2736
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 structures. Trying to comprehend this is harder than I thought it was going to be. I should of just skipped this chapter and went right into pointers since they seem to be easier to use. But anyways here i smy question: you define a structure...
2
1597
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 copies each element individually. It is my understanding that If I just set obj A = B then A only contains a reference to B and doesn't actually make a new copy. I have looked in the help files extensively and
1
257
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 copy the one into the other, can't I? Is there any way to do this? Thanks Peter
10
11402
by: David Rasmussen | last post by:
If I have this struct S { int arr; }; and I do this: S s1,s2;
10
1700
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? This code illustrates what I mean: int main(void) { struct foo { char c; }; struct bar { char c0; char c1; };
21
2539
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 this, and it seems to work fine, but are there any cases where doing something like this could cause any problems? Here's the small program I wrote to test this: #include <stdio.h>
1
1086
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 that I need to create deep copies of my objects, but set some property fields to null in some cases. For example I have an attachment feature which attaches one point of a line to a point of another shape. For that I store a reference to an...
2
2410
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 the change. I know that Structures are value types, sit on the stack, and are generally more efficient to manipulate than reference types (i.e. Classes). Structures cannot use inheritance, the finalize method or default constructors. Can anyone...
2
7208
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 Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.