Re: Struct assignment
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Sat, 30 Jun 2007 18:05:53 +0100
"Grey Alien" <grey@xxxxxxxxxxxxx> wrote in message news:erydna87m6gBFRvbRVnyjwA@xxxxxxxxx
If I have the ff struct: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.
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)
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)?
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
.
- References:
- Struct assignment
- From: Grey Alien
- Struct assignment
- Prev by Date: Re: segmentation fault
- Next by Date: Re: reply the answer
- Previous by thread: Struct assignment
- Next by thread: Re: Struct assignment
- Index(es):
Relevant Pages
|