How sizeof(array) works at runtime?

前端 未结 6 1688
心在旅途
心在旅途 2020-11-28 10:45

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 numb

相关标签:
6条回答
  • 2020-11-28 10:55

    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.

    0 讨论(0)
  • 2020-11-28 10:56

    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.

    0 讨论(0)
  • 2020-11-28 10:56

    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).

    0 讨论(0)
  • 2020-11-28 11:07

    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.

    0 讨论(0)
  • 2020-11-28 11:15

    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.)

    0 讨论(0)
  • 2020-11-28 11:21

    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
    
    0 讨论(0)
提交回复
热议问题