Homepage GitHub

Using void in nested unions


#1

Hi! I have the following situation:

# Service type
test.TypeA
   uint8 cmd_code

   ---

   uint8 resp_code
   # Proper to add void4 here?
   TypeB Payload

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

# Nested type
test.TypeC
   @union
   uint8 xx
   float32 yy
   int32 zz

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

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

Question2:
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.


#3

Ok! Thank for the reply!

Kind Regards