473,583 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

performing a "deep copy" on a nested structure

Hi,

I have the ff data types :

typedef enum {
VAL_LONG ,
VAL_DOUBLE ,
VAL_STRING ,
VAL_DATASET
}ValueTypeEnum ;

typedef union {
long lval ;
double fval ;
char* sval ;
void* ptr ;
} Value ;

typedef struct {
int magic ;
int version ;
}Header ;

typedef struct {
char label[20] ;
id int ;
}Key ;

typedef struct {
Header *hdr ;
char *subject ;
Key key ;
ValueTypeEnum type ;
Value value ;
size_t size ;
}MotherStruct ;
Pseudo code:
=============

MotherStruct *pMS_source = (MotherStruct*) calloc(1, sizeof(MotherSt ruct));

pMS_source->hdr = CreateHeader();
pMS_source->subject = strdup("Test Subject") ;
pMS_source->key = CreateKey("Test ", 2000) ;
pMS_source->type = VAL_STRING ;
pMS_source->value.sval = strdup("Homer Simpson") ;
pMS_source->size = /* How do I calculate this ? */
void MakeMSClone(Mot herStruct *pMS_Dest, const MotherStruct *pMS_source) {
memmove(pMS_des t,pMS_source, pMS_Source->size) ;

}

MTIA

Nov 15 '05 #1
1 3545
Alfonso Morra wrote:
Hi,

I have the ff data types :
Sorry, but I work on the ff library and I don't recognise them ;-)

Seriously, how is anyone meant to know what you mean by ff in the above
statement.
typedef struct {
Header *hdr ;
char *subject ;
Key key ;
ValueTypeEnum type ;
Value value ;
size_t size ;
}MotherStruct ;
<snip>
Pseudo code:
=============
It is better to post actual compilable code, or as close as you can get
to compilable as possible than pseudo code. Obviously when your problem
is "why doesn't this compile" we don't expect it to compile.
MotherStruct *pMS_source = (MotherStruct*) calloc(1, sizeof(MotherSt ruct));
You don't need to cast the result of malloc/calloc and if the compiler
complains when you don't you have done something wrong. Also, you can
use the size of what you are pointing at and simplify it to
MotherStruct *pMS_source = calloc(1, sizeof *pMS_source);
However, calloc sets all bits zero which is not necessarily how a null
pointer is represented
pMS_source->hdr = CreateHeader();
pMS_source->subject = strdup("Test Subject") ;
pMS_source->key = CreateKey("Test ", 2000) ;
pMS_source->type = VAL_STRING ;
pMS_source->value.sval = strdup("Homer Simpson") ;
You have just overwritten all the fields, so why did you bother with
calloc? Why not use malloc and save having the space initialised to all
bits zeros.

Also, strdup is not a standard C function and it is *not* available on
all systems.
pMS_source->size = /* How do I calculate this ? */
That depends on what it is meant to contain.
void MakeMSClone(Mot herStruct *pMS_Dest, const MotherStruct *pMS_source) {
memmove(pMS_des t,pMS_source, pMS_Source->size) ;
Assuming that pMS_dest and pMS_source cannot overlap, which I am
guessing is a requirement, you could use memcpy instead of memmove. I
tend to reserve memmove for when it is legal for the source and
destinations to overlap.

Based on this you wanted
pMS_source->size = sizeof *pMS_source;
However, if that was what you want to use the size of you don't need it
at all since you could do:
memmove(pMS_des t,pMS_source, sizeof *pMS_source);

I think your problem is probably that you have not yet understood that
memmove and memcpy do NOT perform a deep copy. I.e. after the copy
pMS_dest->subject and pMS_source->subject will point to the SAME
location. So if you then did
pMS_source->subject[0]='A';
you would affect both of them.
}

MTIA

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2235
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
0
1608
by: Kevin Schneider | last post by:
Please forgive me if this is a bit off topic, but I haven't had any takers in other forums. With ASP.NET projects, is it possible to have VS.NET automatically change the configuration options of the "Copy Project" dialog box based on the value selected in the "Solution Configurations" drop down list? I'd like to set up the project that...
1
6034
by: Jesper Denmark | last post by:
Hi, Is deep serialization possible using XML. Know any good tutorials?. By deep serialization I mean the ability that an object being serialized automatically serialize its members. E.g Class A {
1
5587
by: Knepper, Michelle | last post by:
Hi out there, I'm a first-time user of the "Copy ... From..." command, and I'm trying to load a table from a text flat file. http://www.postgresql.org/docs/7.4/static/sql-copy.html I don't know if I'm using the command correctly. Question: I run this command as a superuser, and the "copy from" command is run like a SQL command on the...
4
2003
by: lars.uffmann | last post by:
Hey everyone! I am (still) working on a project that I took over from former students, so don't blame me for the criminal approach on coding *g* The problem I have is fairly easy and while I have solutions, none of them seems really "clean" to me and I was wondering if someone here maybe had a better idea: class geometry { ... }
40
3114
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case...
1
3446
by: Russell Warren | last post by:
I just did a comparison of the copying speed of shutil.copy against the speed of a direct windows copy using os.system. I copied a file that was 1083 KB. I'm very interested to see that the shutil.copy copyfileobj implementation of hacking through the file and writing a new one is significantly faster... any clue as to why this is? I...
15
1699
by: Matt Kruse | last post by:
Consider the following two functions: function func1() { var o ; if ( (o=document.forms) && (o=o) && (o=o.elements) && (o=o.sel) && (o=o.options) && (o=o)
3
1950
by: Jess | last post by:
Hello, The C++ reference says the return result of "copy" is an output iterator. I'm wondering how I can assign the returned iterator to some other iterator. I tried int main(){ string s("abcdefg"); vector<charv; vector<char>::iterator k = copy(s.begin(),s.end(),back_inserter(v));
0
7888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7922
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...
0
8185
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...
0
6571
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...
0
5366
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...
1
2317
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
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.