Featured post
c++ - memory alignment issues with union -
is there guarantee, memory object aligned if create object of type in stack?
union my_union { int value; char bytes[4]; };
if create char bytes[4] in stack , try cast integer there might alignment problem. can avoid problem creating in heap, however, there such guarantee union objects? logically there should be, confirm.
thanks.
well, depends on mean.
if mean:
will both
int
,char[4]
members of union aligned may use them independently of each other?
then yes. if mean:
will
int
,char[4]
members guaranteed aligned take same amount of space, may access individual bytes ofint
throughchar[4]
?
then no. because sizeof(int)
not guaranteed 4. if int
s 2 bytes, knows 2 char
elements correspond int
in union
(the standard doesn't specify)?
if want use union access individual bytes of int
, use this:
union { int i; char c[sizeof(int)]; };
since each member same size, they're guaranteed occupy same space. believe want know about, , hope i've answered it.
- Get link
- X
- Other Apps
Comments
Post a Comment