[Ilugc] Structure copy behaviour

  • From: vaidhy@xxxxxxxxxx (Vaidhy)
  • Date: Thu May 24 12:22:26 2007

From what I remember, gcc always does a shallow copy (case 1) and as an
optimization, it implements copy-on-write.

Basically, assigning a[0] = t will have t and a[0] pointed to the same
location till you change either t or a[0]. At that point, there will be a
memcpy for sizeof(tmp) from &t to &a[0] and then the write will happen.

If you want to have a deep copy, only option I can think of right now is to
do it in C++ and implement the copy constructor and also overload the
assignment operator. (this is your case 2).

Regards,
Vaidhy

PS: My C and C++ are kind of rusty since I have not coded in them for the
last 4+ years. If things have changed since then, someone will be able to
correct it :)


On 5/24/07, Sujith Manoharan <m.sujith@xxxxxxxxx> wrote:

Hi,

Say I have a structure :

typedef struct {
        ...
        ...
        ...
} tmp;

And I do this :

tmp a[256];
tmp t;

a[0] = t;

Now, for this structure copy how does gcc decide that :

1) It should generate a call to memcpy
2) Generate instructions to manually do the copy.

What is the threshold for the structure's size that changes gcc's
behaviour ?
And is there any gcc parameter to change this threshold value ?

regards,
Sujith.

_______________________________________________
To unsubscribe, email ilugc-request@xxxxxxxxxxxxx with
"unsubscribe <password> <address>"
in the subject or body of the message.
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Other related posts: