Home > Blockchain >  How can sizeof determine size of an array, if it's length is determined on runtime?
How can sizeof determine size of an array, if it's length is determined on runtime?

Time:08-18

As much as I know, sizeof can only know size of something, if the size is determined at runtime.

int a1;
cin>>a1;
int x2[a1];
cout<<sizeof(x2)/4<<"\n";

If I give input 10, sizeof says the size of the array is:40/4=10 How can sizeof know this?

CodePudding user response:

So in C, sizeof can only know the size on compile-time and in c it can know the size even if it is determined on runtime?

No, it is wrong.

C && C (C VLA is a GCC extension) Compiler will emit the constant value if sizeof is used on something whose size can be determined compiler time, or will emit some code to calculate it at runtime if it is not possible to evaluate it compile time.

  • Related