Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > use memcpy() to copy one structure to another

Reply
Thread Tools

use memcpy() to copy one structure to another

 
 
Sourcerer
Guest
Posts: n/a
 
      06-15-2004
Hi all.

Can I do this to duplicate the contents of struct a in struct b

struct myStruct a, b, *aptr, *bptr;
aptr = a;
bptr = b;
Do something to initialize contents of structure a
memcpy(bptr, aptr, sizeof(myStruct));

Or is not not a good idea to assume the memory used
by my structure is contiguous ?
should I instead do it field by field.

Kev.

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      06-15-2004
Sourcerer wrote:

> Hi all.
>
> Can I do this to duplicate the contents of struct a in struct b
>
> struct myStruct a, b, *aptr, *bptr;
> aptr = a;


Illegal. Did you mean &a?

> bptr = b;


Ditto &b.

> Do something to initialize contents of structure a
> memcpy(bptr, aptr, sizeof(myStruct));
>
> Or is not not a good idea to assume the memory used
> by my structure is contiguous ?


It may have gaps, but sizeof will cover them all. But why not

memcpy( bptr, aptr, sizeof (*bptr) );

if you're set on using memcpy?

> should I instead do it field by field.


What's wrong with

a = b;

?

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
 
Reply With Quote
 
 
 
 
Thomas Pfaff
Guest
Posts: n/a
 
      06-15-2004
Sourcerer <> writes:

> Hi all.
>
> Can I do this to duplicate the contents of struct a in struct b
>
> struct myStruct a, b, *aptr, *bptr;
> aptr = a;
> bptr = b;
> Do something to initialize contents of structure a
> memcpy(bptr, aptr, sizeof(myStruct));


memcpy (&b, &a, sizeof b); or simply b = a;
 
Reply With Quote
 
Sourcerer
Guest
Posts: n/a
 
      06-15-2004
On Tue, 15 Jun 2004 14:13:22 +0100, Chris Dollin <>
wrote:

>Sourcerer wrote:
>
>> Hi all.
>>
>> Can I do this to duplicate the contents of struct a in struct b
>>
>> struct myStruct a, b, *aptr, *bptr;
>> aptr = a;

>
>Illegal. Did you mean &a?
>
>> bptr = b;

>
>Ditto &b.
>
>> Do something to initialize contents of structure a
>> memcpy(bptr, aptr, sizeof(myStruct));
>>
>> Or is not not a good idea to assume the memory used
>> by my structure is contiguous ?

>
>It may have gaps, but sizeof will cover them all. But why not
>
> memcpy( bptr, aptr, sizeof (*bptr) );
>
>if you're set on using memcpy?
>
>> should I instead do it field by field.

>
>What's wrong with
>
> a = b;
>
>?


Wow ! that was fast.

I hadn't realized a = b would do it.

so, I could have

struct mystruct
{
char str[50];
}

struct mystruct a, b;
sprintf(a.str, "Testing 123");
b = a;

and then b.str would also contain "Testing 123" ?


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      06-15-2004
Sourcerer wrote:
> Hi all.
>
> Can I do this to duplicate the contents of struct a in struct b
>
> struct myStruct a, b, *aptr, *bptr;
> aptr = a;

aptr = &a;

> bptr = b;

bptr = &b;

> Do something to initialize contents of structure a
> memcpy(bptr, aptr, sizeof(myStruct));

Try these:
b = a;
memcpy(&b, &a, sizeof(struct myStruct));


>
> Or is not not a good idea to assume the memory used
> by my structure is contiguous ?
> should I instead do it field by field.

I believe that the total space occupied by a structure
is contiguous; however, the compiller is allowed to
add padding between the fields.

When copying one structure to another, memcpy can be
used. But you should have a policy when it comes
to pointer fields:
1. Copy only the pointer and have multiple pointers
to one object.
2. Create a new copy of the target object, thus having
two copies of the target object.
3. Reference counting: multiple pointers to one
object, but the number of references must be zero
before the target object is deleted.

I would copy field by field when copying to or from
a buffer (unsigned char array). Padding bytes may
be copied from one structure to another, but a buffer
may not have padding between the fields.

>
> Kev.
>




--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
Reply With Quote
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      06-15-2004
Sourcerer wrote:

> Hi all.
>
> Can I do this to duplicate the contents of struct a in struct b
>
> struct myStruct a, b, *aptr, *bptr;
> aptr = a;
> bptr = b;
> Do something to initialize contents of structure a
> memcpy(bptr, aptr, sizeof(myStruct));
>
> Or is not not a good idea to assume the memory used
> by my structure is contiguous ?
> should I instead do it field by field.
>
> Kev.


Juste use:
b = a;

- Dario
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-15-2004
Sourcerer wrote:
>
> Can I do this to duplicate the contents of struct a in struct b
>
> struct myStruct a, b, *aptr, *bptr;
> aptr = a;
> bptr = b;
> Do something to initialize contents of structure a
> memcpy(bptr, aptr, sizeof(myStruct));
>
> Or is not not a good idea to assume the memory used
> by my structure is contiguous ?
> should I instead do it field by field.


That better be:

aptr = &a;
bptr = &b;

but you can duplicate the contents by:

b = a;

What you cannot do is check equality etc. by:

if (b == a) ...; /* not allowed */

structs are not arrays.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-15-2004
Sourcerer wrote:
> Hi all.
>
> Can I do this to duplicate the contents of struct a in struct b
>
> struct myStruct a, b, *aptr, *bptr;
> aptr = a;
> bptr = b;
> Do something to initialize contents of structure a
> memcpy(bptr, aptr, sizeof(myStruct));


Why not
struct myStruct a = { /* initialize a */ }, b;
b = a;
and forget all your side issues.
 
Reply With Quote
 
Alex Fraser
Guest
Posts: n/a
 
      06-15-2004
"Martin Ambuhl" <> wrote in message
news:...
> Sourcerer wrote:
> > Hi all.
> >
> > Can I do this to duplicate the contents of struct a in struct b
> >
> > struct myStruct a, b, *aptr, *bptr;
> > aptr = a;
> > bptr = b;
> > Do something to initialize contents of structure a
> > memcpy(bptr, aptr, sizeof(myStruct));

>
> Why not
> struct myStruct a = { /* initialize a */ }, b;
> b = a;
> and forget all your side issues.


Does the assignment above invoke undefined behaviour if not all members of a
are initialised?

What about the "equivalent" memcpy()?

Alex


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-15-2004
Alex Fraser wrote:

> "Martin Ambuhl" <> wrote in message
> news:...
>
>>Sourcerer wrote:
>>
>>>Hi all.
>>>
>>>Can I do this to duplicate the contents of struct a in struct b
>>>
>>>struct myStruct a, b, *aptr, *bptr;
>>>aptr = a;
>>>bptr = b;
>>>Do something to initialize contents of structure a
>>>memcpy(bptr, aptr, sizeof(myStruct));

>>
>>Why not
>> struct myStruct a = { /* initialize a */ }, b;
>> b = a;
>>and forget all your side issues.

>
>
> Does the assignment above invoke undefined behaviour if not all members of a
> are initialised?


Just how, given the presence of an initializer, do you propose to
partially initialized the structure?

> What about the "equivalent" memcpy()?


Who cares about function calls when a simple assignment will do?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selection structure nested within another selection structure 4Ankit@gmail.com Javascript 1 12-07-2006 11:47 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM
Passing value from one script on one page to another script on another page. Robert Cohen ASP General 3 07-15-2003 01:46 PM



Advertisments