4

I know that this question has been asked before, but my question is more specific, here is the code:

#include <stdio.h>
#include <time.h>    /* must be included for the time function */

main()
{
    time_t t = time(NULL);
    srand((unsigned) t);

    int i = rand();

    int a[i];

    printf("%d\n", i);        /* ouptut: 18659 */
    printf("%d\n", sizeof a); /* output: 74636 */
}

I compiled this code using gcc and -ansi option to restrict it to recognize the ANSI C only. I know that there is no way that the compiler could know at compile-time the size of the array because it's determined randomly at run-time. Now my question is is the value returned by sizeof is just a random value, or it has a meaning?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105

4 Answers4

6

The sizeof operator is evaluated at compile time for most operands. In the case of a VLA, it is evaluated at runtime.

From section 6.5.3.4 of the C standard:

2 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

So the size returned by sizeof is the size in bytes of the VLA. In your example, that would be i * sizeof(int)

dbush
  • 205,898
  • 23
  • 218
  • 273
3

The value returned by sizeof is a pseudo-random value that has a meaning:

  • The value represents the size of VLA a, in bytes
  • It is random only in the sense that the number of array elements is determined by a call to rand.

It appears that sizeof(int) on your system is 4, because the number returned by sizeof is four times the number of elements in the array.

Note: One of the consequences of allowing VLAs in C99 is that sizeof is no longer a purely compile-time expression. When the argument of sizeof operator is a VLA, the result is computed at run-time.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

is the value returned by sizeof is just a random value, or it has a meaning?

It does have a meaning, as 74636 / 18659 = 4, which apparently is the size of int on your machine.

So sizeof (which will be computed at run-time instead of compile-time since the argument is a VLA), will return the size of array a in bytes, which is the number of ints it contains (in other words i or 18659) multiplied by the size of int, which is 4 on your machine.

As i (the result of rand) is random, you can consider that the value of sizeof will be random too in that sense.

Marievi
  • 4,951
  • 1
  • 16
  • 33
2

From the gcc manual

The -ansi option does not cause non-ISO programs to be rejected gratuitously. For that, -Wpedantic is required in addition to -ansi.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • That is not really the answer to the question (sizeof(a) valid for VLA), is it? – Aconcagua May 11 '17 at 12:15
  • @Aconcagua actually it is, because it means that sizeof(a) is no more evaluated in compile time only as it is the case in C89 (not using -Wpedantic option). – Maykel Jakson May 11 '17 at 12:19
  • @MaykelJakson The word 'sizeof' is not even mentioned in the answer - neither any word about evaluating it at compile/run time, neither a confirmation that the struct is valid. *This* would have been what makes the right answer - given one answers the question "How to compile ANSI only with GCC?", which OP (incorrectly) assumed already being doing... – Aconcagua May 11 '17 at 15:37