473,836 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
15 38346
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*****@earthl ink.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********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om...
Martin Ambuhl wrote:
Alex Fraser wrote:
"Martin Ambuhl" <ma*****@earthl ink.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.c om (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.c om (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
17521
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 library function? That is, can I be sure that every c++ standard library has this function? Or is there a c++ alternative to it?
10
18658
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 like this? The classes contain no dynamicaly allocated data, just standard types and arrays. Here is an example of what I'm doing, I know in isolation it may seem odd. I am presuming this will copy all data ok but not really sure of the workings and...
6
2953
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 use '&' operator while using memcpy....i have code that use '&' and the code that call memcpy without '&' like is the following same Quest1 ---
3
14312
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 pass over the data...but in memcpy() there are 2 passes..one is for strlen() and another is for copying.. but how memcpy will be faster for large strings? Can anybody please clear my doubt
2
13189
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 is always safer ?
4
3301
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 as follows: public struct MyStruct { public int iOne;
3
6897
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 action, using memcpy Am I correct in assuming that I can't (or shouldn't) because of structure padding? Lets say I've got a struct of {
14
2807
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> #define SIZE_OF_DST 3 int main(void)
11
8153
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
9810
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 usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9656
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10527
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10573
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
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

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.