I know that sizeof
never evaluates its operand, except in the specific case where said operand is a VLA. Or, I thought I knew.
void g(int n) {
printf("g(%d)\n", n);
}
int main(void) {
int i = 12;
char arr[i]; // VLA
(void)sizeof *(g(1), &arr); // Prints "g(1)"
(void)sizeof (g(2), arr); // Prints nothing
return 0;
}
What is going on?
Just in case, this is compiled with GCC 5.1 on Coliru.
It seems that I should think twice before posting, because it struck me right after I did.
My understanding of how
sizeof
interacts with VLAs is actually correct, as the following quote confirms (thanks @this !) :That's not what is causing this surprising (to me) behaviour.
In the
(g(2), arr)
subexpression, the comma operator triggersarr
's array-to-pointer decay. Thus,sizeof
's operand is no longer a VLA, but a plainchar*
, and it falls back to not evaluating its operand.Apparently this behaviour has been altered in C++, where the comma operator won't decay arrays anymore.