26

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 ?

dark_shadow
  • 3,503
  • 11
  • 56
  • 81

6 Answers6

28

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
ouah
  • 142,963
  • 15
  • 272
  • 331
  • What does mean that `int [n++]`? First time I see it in C language – The Mask Jun 09 '13 at 21:45
  • 1
    @TheMask `int [N]` is a C type name. It's an array `N` of `int`. When `N` is not a constant like in the answer, it's a *variable length array*. – ouah Jun 09 '13 at 21:49
  • @ouah : since earlier we have studied that the expression inside sizeof operator doesn't really evaluate. ie. sizeof(n++) will leave n to 5 ( if n=5 initially ). then why value of n is changed here ? – rforritz Jun 12 '13 at 21:27
  • 1
    @rforritz variable length array type operand is an exception to this rule, in this case the operand is evaluated – ouah Jun 12 '13 at 21:33
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.)

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 4
    Put the file you want to compile in a directory by itself, then invoke gcc on it from a shell in that directory, adding `-fdump-tree-all` to the command line options (you probably want `-S` and `-O2` as well). You need to isolate the file in a scratch directory because this will generate approximately 100 files of intermediate code (and that's only the first half of the optimization pipeline; `-fdump-rtl-all` will give you another 60ish files). You can then read through them in sequence (they're numbered) – zwol Jun 19 '13 at 03:02
  • @TheMask (continued) The "tree" dumps are generally more useful than the "rtl" dumps unless you are trying to debug one of GCC's "back ends" (code generation for a specific architecture). You can limit the output to a specific pass of interest by saying `-fdump-(tree|rtl)-PASSNAME` instead of `-all`, where PASSNAME is the word after the number in the dump filename. See http://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Debugging-Options.html#index-d-589 (scroll up a bit, then read from there to the end of the document) for further details. – zwol Jun 19 '13 at 03:07
  • @TheMask ... I should probably also point you at the [internals manual for GCC](http://gcc.gnu.org/onlinedocs/gccint/) which explains what the intermediate representations are and how they work. – zwol Jun 19 '13 at 03:09
  • Thank you so much! I'II check out all this. :) – The Mask Jun 22 '13 at 13:14
8

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.

Anthales
  • 1,158
  • 6
  • 10
4

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Indeed (C11): "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." – pmor Nov 22 '21 at 16:00
2

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

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711