Re: Struct assignment
- From: Thomas Lumley <thomas@xxxxxxxxxxx>
- Date: Sat, 30 Jun 2007 10:14:50 -0700
On Jun 30, 9:43 am, Grey Alien <g...@xxxxxxxxxxxxx> wrote:
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,"test");
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
.
- References:
- Struct assignment
- From: Grey Alien
- Struct assignment
- Prev by Date: Re: Struct assignment
- Next by Date: Re: Machine epsilon: conclusion
- Previous by thread: Re: Struct assignment
- Index(es):
Relevant Pages
|