Variable-length arrays and the max() mess
Variable-length arrays and the max() mess
Posted Mar 14, 2018 14:51 UTC (Wed) by fweimer (guest, #111581)In reply to: Variable-length arrays and the max() mess by zblaxell
Parent article: Variable-length arrays and the max() mess
Not sure what your point is, but const variables are not constant expressions in C. This creates a VLA:
const int count = 5; int array[count];
Posted Mar 14, 2018 17:50 UTC (Wed)
by zblaxell (subscriber, #26385)
[Link]
The original version, annotated:
Variable-length arrays and the max() mess
#define __max(t1, t2, max1, max2, x, y) ({ \
t1 max1 = (x); \ // max1 is a non-const t1
t2 max2 = (y); \ // max2 is a non-const t2
(void) (&max1 == &max2); \
// compute address of a non-const t1
// compute address of a non-const t2
// compare addresses
// don't emit code for the last three lines
// see questions below
max1 > max2 ? max1 : max2; })
At what point do the compiler's pointer-aliasing optimization rules kick in? Does the compiler realize the pointers are not used, or must it assume that because these pointers exist, that max1 and max2 might be modified before the comparison? Does the & operator force the compiler to instantiate max1 and max2, then later elide the code but not forget the side-effect? Does even asking this question cause the compiler to be unable to reduce this to a compile-time constant, and turn this into a VLA instead?