[LLVMdev] zero-sized arrays and alignment

edA-qa mort-ora-y eda-qa at disemia.com
Wed Nov 20 01:47:29 PST 2013


I've done some playing with zero-sized arrays and would like to confirm
that my findings are correct. I can't easily test on a lot of platforms,
nor is this clearly covered in the docs, so I'd like to be sure before I
rely heavily on it.

1. A zero-sized array in a structure will be aligned to the natural
alignment of the contained type. That is, [0 x T] will be aligned
appropriately for T.

2. A zero-sized array in a structure takes up no space.  (ex: sizeof( {
i32, [0 x i8], double ) == sizeof( { i32, double } )

3. The element following a zero-sized array has the same address as the
array if it's alignment requirements are equal or less strict.

If all this is true, which testing shows on the x86_64 platform, then I
can use zero-sized arrays to force the alignment of any structure
element. I don't have to calculate the padding on my own (as does clang).

For example, the C++ struct:
	struct pine {
		int8_t a;
		int8_t alignas(int32_t) b;
	};
Can become:
	type { i8, [0 x i32], i8 }



-- 
edA-qa mort-ora-y
Leaf Creator

Leaf - the language we always wanted
http://leaflang.org/



More information about the llvm-dev mailing list