On 05.12.22 16:54, Marko Mäkelä wrote:
If NumCamSlots is 0, SlotPriority[] is never accessed. So why allocate memory for it if it is never used?
Allocating a variable-length array of length 0 is undefined behaviour. The compiler is allowed to assume NumCamSlots>0 and optimize something based on that assumption.
I think this is because some compilers avoid zero-sized data structures, so that every data structure has an unique address. In this case, if NumCamSlots is 0, the address of SlotPriority would be the same as of NumUsableSlots. (or whatever follows it in case of variable-length local arrays.) The alternative would be to ensure a minimum length of 1 (as its done for empty structs), but that would add notable overhead for a corner case, so its left 'undefined'.
Cheers,
Udo