http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47932

           Summary: __typeof__ prevents VLA from being evaluated
           Product: gcc
           Version: 4.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: [email protected]
        ReportedBy: [email protected]


The program below shows that the __typeof__ macro prevents the evaluation of
the variable-length array in a context where one would expect it to be
evaluated according to the documentation:

    The operand of typeof is evaluated for its side effects if and only if it
is an expression of variably modified type or the name of such a type. 

$ cat <<EOF | gcc -xc - && ./a.out
extern int printf(const char*, ...);

#define A(a)   (sizeof (a) / sizeof ((a)[0]))
#define B(a)   (sizeof (a) / sizeof (__typeof__((a)[0])))

int i;

int f() { printf("%s\n", __func__); return i++; }

void g(int n) {
    int a[n][n][n];

    i = 0;
    printf("%zu\n",  A (a[f()]));
    printf("i = %d\n", i);

    i = 0;
    printf("%zu\n",  B (a[f()]));
    printf("i = %d\n", i);
}

int main () {
    g(3);
}
EOF
f
f
3
i = 2
f
3
i = 1

Reply via email to