简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM