473,902 Members | 6,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 questions about struct copy

Hi, I'm haunted by 2 questions about struct copy. Though I searched the
net, but still in confusion.

1. Does struct assignment copies every member including array members?
For example,

struct A
{
int n;
int m[2];
} ;

struct A a = { 1, {2,3}};
struct A b = { 7, {8,9}};

After b = a; will b.m[0] and b.m[1] have the same value as in a? I
tested this using mingw, and the answer is yes. But i want to know if
this behavior is guaranteed by standard.

2. Does struct assignment equal to memcpy them?

If so, then after struct assignment, to apply memcmp on them will
return 0(equal); otherwise memcmp will return non-zero.

Any guidance?

Mar 3 '06
28 25533
P Boy wrote:
Here http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html is a good source
about struct copying.


I would not recommend that start. It recommends doing things which are
generally considered bad style around here, such as casting the return
value of malloc (I can only remember one person on this group with a
good reason to do this, and his reason does not apply to most people)
and using sizeof(type) rather than sizeof *ptr to work out how much
space to allocate. I.e. the web page recommends using, and I quote:
ptr = (*int) malloc(sizeof(i nt)*N)
The generally recommended style around here is the far simpler and more
maintainable form:
ptr = malloc(N * sizeof *ptr);
Fewer characters to type, remains correct of the type of ptr changes,
and does not hide the required diagnostic if there is no declaration of
malloc in scope.

It also goes on to recommend using #define to create an alias for a
type, which is what the typedef keyword is for, and typedef has
advantages over a #define.

Also, please quote some context when replying so that people know what
you are replying to. It is entirely possible that some people might see
your reply but not the original message. See
http://cfaj.freeshell.org/google/ for more about this problem and how to
post correctly through Google. Also see my sig for a link to a lot
more information on what is considered acceptable in the group and why.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Mar 5 '06 #21
Micah Cowan <mi***@cowan.na me> writes:
Except that the above text says that the value of a /member/ of the
structure or union object may be a trap representation. It will have
been read in the assignment, I should think.


Keith Thompson wrote: Not necessarily. Structure assignment *may* be done as if by calling
memcpy(), i.e., copying the raw bytes; this would avoid any problems
from members with trap representation. I *think* that structure
assignment *must* be done this way, or in some equivalent manner
(except that padding bytes needn't be copied).

It's common for some structure members to be meaningless depending on
the values of other members.


That's a good point.

Can we therefore conclude that structure assignment cannot be done
as member-by-member assignments on systems that allow trap
representations , on the theory that assignment of a member having
a trap value incurs undefined behavior (or at least affects the overall
struct assignment operation)?

-drt

Mar 6 '06 #22
"David R Tribble" <da***@tribble. com> writes:
Micah Cowan <mi***@cowan.na me> writes:
Except that the above text says that the value of a /member/ of the
structure or union object may be a trap representation. It will have
been read in the assignment, I should think.


Keith Thompson wrote:
Not necessarily. Structure assignment *may* be done as if by calling
memcpy(), i.e., copying the raw bytes; this would avoid any problems
from members with trap representation. I *think* that structure
assignment *must* be done this way, or in some equivalent manner
(except that padding bytes needn't be copied).

It's common for some structure members to be meaningless depending on
the values of other members.


That's a good point.

Can we therefore conclude that structure assignment cannot be done
as member-by-member assignments on systems that allow trap
representations , on the theory that assignment of a member having
a trap value incurs undefined behavior (or at least affects the overall
struct assignment operation)?


I think it means that the assignment can be done in any way the
implementation chooses, as long as any defined values of members in
the source are properly copied to the target, and as long as any
member trap representations in the source don't cause anything "bad"
to happen (defining "bad" is left as an exercise).

Accessing a trap representation invokes undefined behavior, but the
implementation is allowed to do so if it knows it won't cause any
problems.

The obvious way to do this is to use the equivalent of memcpy(). In
fact, it seems to me it would have been reasonable to *require* struct
assignment to be done as if by memcpy(). (An implemention might save
some time by not copying uninitialized members, but I doubt that any
real implementation actually does this; the time saved by not copying
padding bytes is unlikely to be significant.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 6 '06 #23
In comp.std.c David R Tribble <da***@tribble. com> wrote:

Can we therefore conclude that structure assignment cannot be done
as member-by-member assignments on systems that allow trap
representations , on the theory that assignment of a member having
a trap value incurs undefined behavior (or at least affects the overall
struct assignment operation)?


No, because your theory doesn't necessarily hold. An implementation is
free to use member-by-member assignment if it knows, or can ensure, that
no untoward behavior will actually occur.

-Larry Jones

Some people just don't have inquisitive minds. -- Calvin
Mar 6 '06 #24
[struct copy]

Keith Thompson wrote:
[...]
The obvious way to do this is to use the equivalent of memcpy(). In
fact, it seems to me it would have been reasonable to *require* struct
assignment to be done as if by memcpy(). (An implemention might save
some time by not copying uninitialized members, but I doubt that any
real implementation actually does this; the time saved by not copying
padding bytes is unlikely to be significant.)


Pathological cases can be devised:

struct Foo
{
unsigned m1: 8;
unsigned :24;

unsigned m2: 8;
unsigned :24;

unsigned m3: 8;
unsigned :24;

unsigned m4: 8;
unsigned :24;
};

You could make the argument that copying only the first 8 bits of each
32-bit chunk of the structure could be faster than copying each entire
32-bit chunk.

-drt

Mar 7 '06 #25

Keith Thompson wrote:
Micah Cowan <mi***@cowan.na me> writes:
Keith Thompson <ks***@mib.or g> writes:
"Robin Haigh" <ec*****@leeds. ac.uk> writes: [...] > While on the subject, I'm not clear as to whether the above quote
> answers a question I have. The question is: if we have an auto
> struct, is it required that all members be initialised before the
> struct can be copied by assignment? If not, where does it say
> so?

I think this is covered by the statement that the value of a structure
or union object is never a trap representation. If there are no trap
representations , there should be no problems with assignment.


Except that the above text says that the value of a /member/ of the
structure or union object may be a trap representation. It will have
been read in the assignment, I should think.


Not necessarily. Structure assignment *may* be done as if by calling
memcpy(), i.e., copying the raw bytes; this would avoid any problems
from members with trap representation. I *think* that structure
assignment *must* be done this way, or in some equivalent manner
(except that padding bytes needn't be copied).

It's common for some structure members to be meaningless depending on
the values of other members. For example:

#include <stddef.h>
int main(void)
{
#define MAX_LEN 42
struct foo {
int len;
void *ptrs[MAX_LEN];
};

struct foo obj1, obj2;
obj1.len = 1;
obj1.ptrs[0] = NULL;

obj2 = obj1;
return 0;
}

Allowing structure assignment to be affected by member trap
representations would cause the assignment to invoke undefined
behavior.


Thanks for all the replies. Now I think I become more acquainted with
struct assignment.

I notice the term "trap representation" appears. I don't know exactly
what it means. Would you please give me some hints?

Mar 9 '06 #26
"WaterWalk" <to********@163 .com> writes:
[...]
I notice the term "trap representation" appears. I don't know exactly
what it means. Would you please give me some hints?


C99 6.2.6.1p5:

Certain object representations need not represent a value of the
object type. If the stored value of an object has such a
representation and is read by an lvalue expression that does not
have character type, the behavior is undefined. If such a
representation is produced by a side effect that modifies all or
any part of the object by an lvalue expression that does not have
character type, the behavior is undefined. Such a representation
is called a _trap representation_ .

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 9 '06 #27
Keith Thompson wrote:
"WaterWalk" <to********@163 .com> writes:
I notice the term "trap representation" appears. I don't know exactly
what it means. Would you please give me some hints?

C99 6.2.6.1p5:
Certain object representations need not represent a value of the
object type. If the stored value of an object has such a
representation and is read by an lvalue expression that does not
have character type, the behavior is undefined. If such a
representation is produced by a side effect that modifies all or
any part of the object by an lvalue expression that does not have
character type, the behavior is undefined. Such a representation
is called a _trap representation_ .


And the name is meant to suggest that the undefined behavior
might be as nasty as a hardware trap, although that actually
occurs only on relatively few platforms. The main thing is
that when a program produces a trap rep the C implementation
doesn't have to handle it "properly" (where "proper" describes
any of the behaviors a programmer might have been expecting).
For example, generated code might depend on a convention that
certain bits of the representation are always 0, so violating
that assumption causes incorrect computation.
Mar 9 '06 #28
Douglas A. Gwyn wrote:
Keith Thompson wrote:
"WaterWalk" <to********@163 .com> writes:
I notice the term "trap representation" appears. I don't know exactly
what it means. Would you please give me some hints?


C99 6.2.6.1p5:
Certain object representations need not represent a value of the
object type. If the stored value of an object has such a
representation and is read by an lvalue expression that does not
have character type, the behavior is undefined. If such a
representation is produced by a side effect that modifies all or
any part of the object by an lvalue expression that does not have
character type, the behavior is undefined. Such a representation
is called a _trap representation_ .

And the name is meant to suggest that the undefined behavior
might be as nasty as a hardware trap, although that actually
occurs only on relatively few platforms. The main thing is
that when a program produces a trap rep the C implementation
doesn't have to handle it "properly" (where "proper" describes
any of the behaviors a programmer might have been expecting).
For example, generated code might depend on a convention that
certain bits of the representation are always 0, so violating
that assumption causes incorrect computation.


Hardware traps aren't all that unusual. Many systems will
trap if given a `long*' with a few stray "low-order" one-bits.
Some IEEE floating-point systems will respond to "signalling "
operands by generating traps, although I'll grant that most C
implementations appear to prefer operating their IEEE units in
a non-trapping mode. VAX processors had/have a different F-P
scheme that traps on certain invalid operands.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 10 '06 #29

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

Similar topics

4
1818
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do assignment, should copy constructor and assignment operator be unnecessary? 2. If a base class have a pointer member, should its derived classes also need copy constructor and assignment operator, no matter if a derived class itself has a...
36
4455
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it but I don't want to. Arrays cannot be assigned in C, but structs can, so I coded the following: #include <stdlib.h>
0
9997
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
9845
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
11279
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
10870
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
10981
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
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5893
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
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4725
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.