How sizeof (array) works at runtime?

I read that the sizeof operator in C is interpreted at compile time, and since at compile time the compiler knows the size of the array and its type, sizeof abled to calculate the number of bytes occupied by the array. But how sizeof works 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 the size of the array is unknown at compile time, then how does it work correctly?

+23
c sizeof
Apr 09 2018-12-12T00:
source share
6 answers

sizeof always computed at compile time in C89. Because C99 and variable-length arrays are computed at runtime when the variable-length array is part of the expression in the sizeof operand.

The same is for evaluating the sizeof operand: it is not evaluated on C89, but on 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 
+27
Apr 09 '12 at 19:08
source share

Since you apply sizeof to a variable-length array whose size is not completely known at compile time, the compiler must generate code to make it part at runtime.

gcc 4.6.3 high-level optimizers convert the code you specify in

 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 this explain what is happening under the hood? (Don’t be distracted by the stupid amount of temporary variables β€” that the artifact of the program, which is in a static single assignment , is formed at the point where I asked for an intermediate code dump.)

+13
Apr 09 '12 at 19:09
source share

From the C99 standard:

6.5.3.4

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

+7
Apr 09 '12 at 19:11
source share

In this case, sizeof() is evaluated at runtime. The compiler, since it knows that the size of a based on the value of n during the declaration of the array, generates code to use the corresponding value of n to return a reasonable value for sizeof() .

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

+2
Apr 09 2018-12-12T00:
source share

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

sizeof determined at compile time in all cases except VLA. For VLA, sizeof is evaluated at runtime.

+2
Apr 09 '12 at 19:08
source share

Regardless of whether sizeof computed at compile time or at runtime (or more formally, whether its result is an integer constant expression), the result of sizeof based solely on the type of its argument , and not any hidden data that accompanies an array of variable length. Of course, when sizeof is applied to a mutable type variant, the created program must keep track of that size somewhere. But he could just recount it if the expression was simple enough, and the variables originally deduced from the length could not be changed. Or it can store the type size somewhere (essentially in a hidden local variable), but it will not be associated with the VLA in any observable way (for example, if you pass a pointer to the first VLA element to another function, this pointer cannot be used to restore the length of the VLA).

+1
Apr 09 '12 at 19:25
source share



All Articles