sizeof(array) with variable-length array parameter

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dear gcc users and developers, 

  This might be a stupid question, nevertheless...

  I've been wondering for a long time, why the behaviour of
variable-length arrays w.r.t. the sizeof operator is different
for local/auto variables and for function arguments (in C99):

#include <stdio.h>
void foo(int s, int a[s]) {
        printf("%u\n",sizeof a);
}
int main()
{
        int s=10,a[s];
        printf("%u\n",sizeof a);
        foo(sizeof a/sizeof a[0],a);
}

  The printf's produce very different results: the first one
returns "40" the other one returns 4, implying that the compiler
forgets that the size of the array is actually "s", not the size
of the pointer argument. Is it so difficult to make "sizeof a" 
return "s" in both cases?

  The info page on gcc-4.3 does refer to variable-length arrays
in the context of the function declaration:

"The length of an array is computed once when the storage is allocated
and is remembered for the scope of the array in case you access it
with `sizeof'."

  This behaviour is fortunately consistent with all versions of gcc
that I actively use (2.95-4.3), so this might be a C99 feature that
is not implemented correctly...

Regards,

Pjotr Kourzanov

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux