473,605 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct assignment

If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:

a.i = 42 ;
strcpy(a.s,"tes t");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b. Question is:

1). is b.s now ptr to a.s ? (I think so)
If so, what happens if for instance variable 'a' goes out of scope (?)

2). Does the compiler generate an implicit "memcpy" or "memmove" behind
the scenes when it sees an assignment like this (to avoid dangling ptrs)?
Jun 30 '07 #1
24 29376

"Grey Alien" <gr**@andromeda .comwrote in message
news:er******** *************@b t.com...
If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:

a.i = 42 ;
strcpy(a.s,"tes t");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b. Question is:

1). is b.s now ptr to a.s ? (I think so)
If so, what happens if for instance variable 'a' goes out of scope (?)

2). Does the compiler generate an implicit "memcpy" or "memmove" behind
the scenes when it sees an assignment like this (to avoid dangling ptrs)?
The answer is 2. A structure assignment will cause a call to memcpy to be
made, or equivalent code emitted if the structure is small enough to make
this wasteful and the compiler is clever.
However if structures contain pointers then the values of the pointers are
overwritten. So you have to be extremely careful not to orphan memory or
create peculiar bugs with aliases.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 30 '07 #2
On Jun 30, 9:43 am, Grey Alien <g...@andromeda .comwrote:
If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];

} a, b;

And use them in code like this:

a.i = 42 ;
strcpy(a.s,"tes t");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b.
Question is:

1). is b.s now ptr to a.s ? (I think so)
I think you are confusing arrays and pointers. Since a.s is an array,
a.s[0] to a.s[LONG_ENOUGH-1] are actually stored in the structure and
are copied by the assignment.

If a.s were just a pointer to memory allocated elsewhere the
assignment would just copy the pointer.
2). Does the compiler generate an implicit "memcpy"
or "memmove" behind the scenes when it sees an
assignment like this (to avoid dangling ptrs)?
There is an explicit copy since a.s is an array. If a.s
were a pointer there would not be an implicit copy, and
Bad Things such as dangling pointers could result.

-thomas

Jun 30 '07 #3
Grey Alien wrote, On 30/06/07 17:43:
If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:

a.i = 42 ;
strcpy(a.s,"tes t");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b.
It is not a bitwise copy, it is a copy of all the elements in the struct.
Question is:

1). is b.s now ptr to a.s ? (I think so)
Since b.s was not defined as a pointer, what makes you think an
assignment could magically transform it from being an array in to being
a pointer? You need to read section 6 of the comp.lang.c FAQ at
http://c-faq.com/ specifically the questions dealing with whether
pointers and arrays are the same thing.
If so, what happens if for instance variable 'a' goes out of scope (?)

2). Does the compiler generate an implicit "memcpy" or "memmove" behind
the scenes when it sees an assignment like this (to avoid dangling ptrs)?
It is very rare for C to do things behind your back. Had there been
pointers in your struct (which there were not) then after the assignment
they would point to the same place as they point in the original struct,
and when that place is no longer valid (an automatic that goes out of
scope, for example) the pointers are no longer valid.
--
Flash Gordon
Jun 30 '07 #4

"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
news:hh******** ****@news.flash-gordon.me.uk...
Grey Alien wrote, On 30/06/07 17:43:
>If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:

a.i = 42 ;
strcpy(a.s,"te st");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b.

It is not a bitwise copy, it is a copy of all the elements in the struct.
Question is:

1). is b.s now ptr to a.s ? (I think so)

Since b.s was not defined as a pointer, what makes you think an assignment
could magically transform it from being an array in to being a pointer?
You need to read section 6 of the comp.lang.c FAQ at http://c-faq.com/
specifically the questions dealing with whether pointers and arrays are
the same thing.
Its easy to see where the confusion comes from. There are situations where
pointers degenerate into pointers , the OP probably had that in mind

Jun 30 '07 #5
Serve Lau said:

<snip>
Its easy to see where the confusion comes from. There are situations
where pointers degenerate into pointers ,
Pointers always degenerate into pointers.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 30 '07 #6
Serve Lau wrote, On 30/06/07 19:53:
"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
news:hh******** ****@news.flash-gordon.me.uk...
>Grey Alien wrote, On 30/06/07 17:43:
<snip>
>>1). is b.s now ptr to a.s ? (I think so)
Since b.s was not defined as a pointer, what makes you think an assignment
could magically transform it from being an array in to being a pointer?
You need to read section 6 of the comp.lang.c FAQ at http://c-faq.com/
specifically the questions dealing with whether pointers and arrays are
the same thing.

Its easy to see where the confusion comes from. There are situations where
pointers degenerate into pointers , the OP probably had that in mind
In my opinion it is only easy to confuse arrays and pointers if it is
badly taught. If arrays and pointers are taught as fundamentally
different concepts and *then* the way array names degenerate to pointers
to the first element is explained there will not be anything like the
problems. In one-to-one sessions I've been able to explain the basics of
arrays pointers to non-computer people in minutes (I needed to so I
could explain approximately what was the cause of a problem), although I
did not go on to how things are done in C.
--
Flash Gordon
Jun 30 '07 #7
Flash Gordon <sp**@flash-gordon.me.ukwri tes:
Grey Alien wrote, On 30/06/07 17:43:
>If I have the ff struct:
struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:
a.i = 42 ;
strcpy(a.s,"te st");
b.i = 100 ;
b = a ;
at this point, a (bitwise?) copy of a is made to b.

It is not a bitwise copy, it is a copy of all the elements in the struct.
Yes -- and that can be, and commonly is, implemented as a bitwise copy
of the struct.

Suppose there's a gap between the members "i" and "s". After the
assignment "b = a;", the gaps in "a" and "b" may or may not have the
same contents. The assignment can be done either as a bitwise copy or
by copying the members one-by-one, leaving any gaps alone.

99% of the type, this doesn't matter because you're never going to
look at what's in the gaps anyway.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 30 '07 #8
Richard Heathfield wrote, On 30/06/07 20:56:
Serve Lau said:

<snip>
>Its easy to see where the confusion comes from. There are situations
where pointers degenerate into pointers ,

Pointers always degenerate into pointers.
No they don't, they stay as pointers ;-)

Knowing what Serve Lau must have intended I read it as "where arrays
degenerate...".
--
Flash Gordon
Jun 30 '07 #9
Flash Gordon said:
Richard Heathfield wrote, On 30/06/07 20:56:
>Serve Lau said:

<snip>
>>Its easy to see where the confusion comes from. There are situations
where pointers degenerate into pointers ,

Pointers always degenerate into pointers.

No they don't, they stay as pointers ;-)

Knowing what Serve Lau must have intended I read it as "where arrays
degenerate...".
I know. Actually, arrays never actually do that. We often say they do,
but we're just being lazy. What we really mean is that the name of an
array, when used in an expression, is often (indeed, *usually*) treated
as if it were a pointer to the array's first element. The array itself
never "degenerate s" (or "decays", as it is usually put) at all.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 30 '07 #10

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

Similar topics

6
3207
by: William J. Leary Jr. | last post by:
I've got a history question, and several of you seem familiar with the history of C, so perhaps someone knows. The following: #include<stdio.h> struct checkit { int a; int b; char c;
2
1535
by: ehui928 | last post by:
/* test.c */ struct S { int a; int b; }; /* The following 3 statements will cause compile error
5
4035
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values before fwrite(), to shut up memory debuggers like Valgrind about writing uninitialized data to the file. Simplified code: static const struct S default_value = ...; struct S s, t;
5
2012
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
1
1514
by: sanddune008 | last post by:
Hi All, 1. How to acess the member name. 2. I am trying to fill the name with "Sanny". Vc++ IDE generates exception on \return. 3. what is wrong with following assignment. struct profile {
0
7938
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
8430
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
8427
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
8078
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
8298
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...
1
5892
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5452
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();...
1
2441
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
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.