0

I was trying to understand the working of sizeof operator and I came across this question. Following is the code from that question

#include <stdio.h>

int main() {
    int i = 0;
    int a[i];
    printf("%zu\n",sizeof(a[i++]));
    printf("%d\n",i); // Here, print 0 instead of 1
    return 0;
}

array a here is variable length but when its used as an oprand to the sizeof operator the variable i is not incremented.

some of the comments and answer say that a[i++] is not a VLA type and suggest that op should use a 2D VLA to see the side effect( sizeof evalutes its oprand).

I dont clearly understand why a[i++] doesn't qualify as a VLA expression.i think it has something to do with the fact that we can leave the first dimension unspecified of an array while passing to a fucntion.

so the question is in general what is considered a VLA expression ?

user2736738
  • 30,591
  • 5
  • 42
  • 56
mightyWOZ
  • 7,946
  • 3
  • 29
  • 46

1 Answers1

5

I don't clearly understand why a[i++] doesn't qualify as a VLA expression

a[n] is not a VLA expression, even though a is a VLA, because the expression produces a single element of a VLA, not the VLA itself.

in general what is considered a VLA expression?

In your example, a would be a VLA expression, because it represents a variable-length array.

A 2D VLA would give you a scenario where sizeof would evaluate its operand:

int x, y;
scanf("%d %d", &x, &y);
int a[x][y];
int i = 0;
size_t z = sizeof(a[i++]);
printf("%d %d\n", i, (int)z);

This prints 1, because now not only a is a VLA, but a[n] is a VLA as well (demo).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    @coderredoc You are right, it looks like OP wants a scenario to demonstrate a situation when `sizeof` would evaluate its operand. Thank you very much! – Sergey Kalinichenko Feb 26 '18 at 19:22
  • do you have any clue why the standard requires the operand to be evaluated? – tstanisl Sep 08 '21 at 21:41
  • @tstanisl my best guess is that it's required for consistency. They could have said "may be evaluated" instead of "is evaluated" to allow implementations to use heuristics when possible, but it would make using the language harder for the programmers (not that the standard writers were particularly accommodating to programmers in the first place, but that's beside the point). – Sergey Kalinichenko Sep 09 '21 at 01:01
  • if their intention was to make language simpler then they failed. They actually made language more complex, they made VLA harder to use, they introduced UB to common idioms like `sizeof *x`. They even didn't fix the confusion because "size expression" which only part of expression that impacts the type, still may or may not be evaluated. See last sentence from 6.7.6.2p5. This refers both to `sizeof` for VLA and non-VLA! Non-VLA sizeof still may have side effects! – tstanisl Sep 09 '21 at 06:37
  • @tstanisl I agree, adding VLAs created more confusion than its worth. That's probably why [VLAs never made it to the C++ standard](https://stackoverflow.com/a/1887178/335858). – Sergey Kalinichenko Sep 09 '21 at 13:56
  • I thinks it's not exactly VLAs fault. VLAs and VM types in general they provide elegant solution to many problems in API design for numerical code. The problem is the wording in "the type of the operand is a variable length array type, the operand is evaluated", which makes no sense after a deeper look – tstanisl Sep 09 '21 at 14:28