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

use memcpy() to copy one structure to another

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.

Nov 14 '05 #1
15 37983
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
Nov 14 '05 #2
Sourcerer <no****@donteventhinkaboutspammingme.xyz> 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;
Nov 14 '05 #3
On Tue, 15 Jun 2004 14:13:22 +0100, Chris Dollin <ke**@hpl.hp.com>
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" ?
Nov 14 '05 #4
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

Nov 14 '05 #5
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
Nov 14 '05 #6
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?
Nov 14 '05 #7
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.
Nov 14 '05 #8
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2j************@uni-berlin.de...
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
Nov 14 '05 #9
Alex Fraser wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2j************@uni-berlin.de...
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?
Nov 14 '05 #10
Alex Fraser wrote:
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?

If an array is partially intialized, the rest of the elements are
default initialized. So no undefined behavior, as all elements will have
values.

Brian Rodenborn
Nov 14 '05 #11
Martin Ambuhl wrote:
Alex Fraser wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
.... snip ...

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?


structures do not have to be initialized at declaration time.
Since a structure with an uninitialized component is an
uninitialized structure, copying it via structure assignment
should be undefined behaviour. Yet I expect it will go unpunished
on most machines other than the DS9000.

--
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?
Nov 14 '05 #12
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
Martin Ambuhl wrote:
Alex Fraser wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
... snip ...
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?


structures do not have to be initialized at declaration time.
Since a structure with an uninitialized component is an
uninitialized structure, copying it via structure assignment
should be undefined behaviour. Yet I expect it will go unpunished
on most machines other than the DS9000.


Thank you, that makes sense. I was thinking in practical terms when I
phrased the question: assigning a structure where the source is partially
initialised is much more likely to happen than where it is completely
uninitialised, but the effect is identical in either case.

I suppose, in fact, it is no different to:

int a, b; /* uninitialised */
b = a; /* undefined behaviour (since a may be a trap representation?) */

But I wonder if the same still applies with the notionally equivalent
memcpy(). I think that question boils down to:

unsigned char a, b; /* uninitialised */
b = a; /* undefined behaviour? */

Alex
Nov 14 '05 #13
kal
"Alex Fraser" <me@privacy.net> wrote in message news:<2j************@uni-berlin.de>...
int a, b; /* uninitialised */
b = a; /* undefined behaviour (since a may be a trap representation?) */


I suppose anything is possible. It may be that the type "int"
is 33 bits in size and at declaration time bit 32 is set to 1.
It is reset to zero only when a value is explicitly assigned to
the variable. Any attempt to read the contents of the variable
when bit 32 is not zero terminates the program execution.

But for us bozos who make do with 16 or 32 bit integers, all bit
patterns represent valid integer values. Hence, unitialized does
not means "undefined" or "trap representation", it just means that
the value in it is not the one we want to start with.

Can someone expound on "trap representation" vis-a-vis integers?
Nov 14 '05 #14
k_*****@yahoo.com (kal) wrote:
"Alex Fraser" <me@privacy.net> wrote in message news:<2j************@uni-berlin.de>...
int a, b; /* uninitialised */
b = a; /* undefined behaviour (since a may be a trap representation?) */
I suppose anything is possible. It may be that the type "int"
is 33 bits in size and at declaration time bit 32 is set to 1.
It is reset to zero only when a value is explicitly assigned to
the variable. Any attempt to read the contents of the variable
when bit 32 is not zero terminates the program execution.


That's one possibility, yes.
But for us bozos who make do with 16 or 32 bit integers, all bit
patterns represent valid integer values.
Nope. Usually, but not necessarily. That is, yes, INT_MAX must be so
large that a 16-bit int needs to use all bits as value or sign bits, but
a 32-bit int is allowed to have (e.g.) 30 value bits, one sign bit, and
one trap bit.
Hence, unitialized does not means "undefined" or "trap representation",
it just means that the value in it is not the one we want to start with.


No. It means "the value is undefined and may be illegal". It does not
mean "the value _must_ be illegal"; it does not mean "the value _cannot_
be illegal"; it means "you do not know whether the value is legal or
not".

Richard
Nov 14 '05 #15
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
"Alex Fraser" <me@privacy.net> wrote in message news:<2j************@uni-berlin.de>...
int a, b; /* uninitialised */
b = a; /* undefined behaviour (since a may be a trap representation?) */
I suppose anything is possible. It may be that the type "int"
is 33 bits in size and at declaration time bit 32 is set to 1.
It is reset to zero only when a value is explicitly assigned to
the variable. Any attempt to read the contents of the variable
when bit 32 is not zero terminates the program execution.


This is an excellent example of trap representation vis-a-vis integers.
But for us bozos who make do with 16 or 32 bit integers, all bit
patterns represent valid integer values.
This is not necessarily true. Even if all the bits are actually used
by the representation, the standard allows -0 for one's complement and
sign-magnitude to be a trap representation, as well as the representation
with the sign bit set and all the value bits reset for two's complement
(normally, this is corresponding to type_MIN).
Hence, unitialized does
not means "undefined" or "trap representation", it just means that
the value in it is not the one we want to start with.
According to the standard, it is an unspecified value, and this includes
trap representations.
Can someone expound on "trap representation" vis-a-vis integers?


All the trap representations not involving padding bits (bits that do not
contribute to the value) have already been described above. Once you
have padding bits, they can have the effect you have already explained.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #16

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

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
10
by: spoc | last post by:
I have been using memcpy to copy one class to another of the same type. There are reasons why I had to do this bug am getting some odd crashes and maybe I'm doing something dodgy copying classes...
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to...
3
by: naren | last post by:
Iam not getting the correct pros and cons of the strcpy() and memcpy() some where i read for short strings strcpy is faster and for large strings memcpy is faster.. in strcpy() there is a single...
2
by: subramanian100in | last post by:
Suppose I need to assign one structure to another. Should each member of the structure be assigned individually or can memcpy be used ? Since the compiler can add padding bytes, which approach...
4
by: | last post by:
In C++ I use the memcpy function to copy a struct to a buffer, I have not been able to find a method to do this in C#. I will need to be able to do this for a struct with mixed types. Example...
3
by: Nope | last post by:
LO everybody, I have a string (defined as a character array), and a structure which comprises of unsigned chars, chars or char arrays. I want to copy the 'string' into the structure, in one...
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
11
by: mthread | last post by:
Hi, I would like to know if C++ has a class equivalent to memcpy and its associated functions available in C.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.