Homepage GitHub

Using void in nested unions


Hi! I have the following situation:

# Service type
   uint8 cmd_code


   uint8 resp_code
   # Proper to add void4 here?
   TypeB Payload

# Nested type
  # Adding void here is not allowed, compile error, hence the union
  uint8 x
  TypeC foo
  TypeC bar
  uint8 y

# Nested type
   uint8 xx
   float32 yy
   int32 zz

TypeC will create a 2bit tag and needs outer void6 to be aligned.

How do I align the data properly, hence I cannot add void into a type with union?

In test.TypeB there are two TypeC included. They will totally add up needing void12, is it enough to add void4 (modulo8) in test.TypeA for proper alignment?

Kind regards

(Pavel Kirienko) #2

Either put voids directly before your unions as you did above or create wrapper types, where the wrapper type would contain simply your union prefixed with a void.

Use wrapper types here. This is the only way to implement robust alignment here.

That said, alignment is not required. If your data structures are small and simple, you can simply leave fields unaligned, it’s perfectly acceptable.


Ok! Thank for the reply!

Kind Regards