简体   繁体   中英

How sizeof(array) works at runtime?

I have read that sizeof operator in C is interpreted at compile time and since at compile time compiler knows the array size and its type,sizeof is abled to compute the number of bytes occupied by array.But how is sizeof working for the following code:

 #include<stdio.h>
 #include<string.h>
 int main()
 {
    int n;
    scanf("%d",&n);
    int a[n];
    int s=sizeof(a);
    printf("%d",s);
    return 0;
 }

Here array size is not known at compile time,then how is it working properly?

sizeof is always computed at compile time in C89. Since C99 and variable length arrays, it is computed at run time when a variable length array is part of the expression in the sizeof operand.

Same for the evaluation of the sizeof operand: it is not evaluated in C89 but in C99 if the operand is of variable length array type it is evaluated. For example:

int n = 5;
sizeof (int [n++]); 

// n is now 6

Since you are applying sizeof to a variable-length array, whose size is not fully known at compile time, the compiler must generate code to do part of it at runtime.

gcc 4.6.3's high level optimizers convert the code you showed to

scanf ("%d", &n);
t12 = (long unsigned int) n;
t13 = t12 * 4;
__builtin_alloca (t13);
t16 = (unsigned int) t12;
t17 = t16 * 4;
s18 = (int) t17;
printf ("%d", s18);

Does that explain what is going on under the hood? (Don't be put off by the silly-looking number of temporary variables -- that's an artifact of the program being in static single assignment form at the point where I asked for an intermediate-code dump.)

From the C99 standard:

6.5.3.4

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated ; otherwise, the operand is not evaluated and the result is an integer constant.

In that case, sizeof() is evaluated at run time. The compiler, because it knows that the size of a is based on the value of n at the time of the array declaration, generates code to use the appropriate value of n to return a sensible value for sizeof() .

In C99, not all uses of sizeof() can be completely evaluated at compile time and reduced to a runtime constant.

I have read that sizeof operator in C is interpreted at compile time

sizeof is determined at compile time in all cases apart from for VLAs. For a VLA, sizeof is evaluated at runtime.

Regardless of whether sizeof is computed at compile time or runtime (or more formally speaking, whether its result is an integer constant expression), the result of sizeof is purely based on the type of its argument and not any hidden data accompanying the variable-length array itself. Of course when sizeof is applied to a variably-modified type, the generated program must keep track of that size somewhere. But it might simply recompute it if the expression was simple enough and the variables it originally derived the length from cannot have changed. Or, it could store the size of the type somewhere (essentially in a hidden local variable), but this would not be connected to the VLA object in any observable way (for example, if you pass a pointer to the first element of the VLA to another function, that pointer cannot be used to recover the VLA length).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM