473,324 Members | 2,268 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,324 software developers and data experts.

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(MotherStruct));

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(MotherStruct *pMS_Dest, const MotherStruct *pMS_source) {
memmove(pMS_dest,pMS_source, pMS_Source->size) ;

}

MTIA

Nov 15 '05 #1
1 3476
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(MotherStruct));
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(MotherStruct *pMS_Dest, const MotherStruct *pMS_source) {
memmove(pMS_dest,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_dest,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
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
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...
1
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 ...
1
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...
4
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...
40
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...
1
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...
15
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.