memory alignment issues with union

Posted by confucius on Stack Overflow See other posts from Stack Overflow or by confucius
Published on 2010-12-21T05:02:46Z Indexed on 2010/12/21 5:21 UTC
Read the original article Hit count: 314

Filed under:
|
|

Hi all,

Is there guarantee, that memory for this object will be properly aligned if we create this object of this type in stack?

union my_union
{
  int value;
  char bytes[4];
};

If we create char bytes[4] in stack and then try to cast it to integer there might be alignment problem. We can avoid that problem by creating it in heap, however, is there such guarantee for union objects? Logically there should be, but I would like to confirm.

Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about union