Array size at run time without dynamic allocation is allowed?

前端 未结 8 1678
一个人的身影
一个人的身影 2020-11-22 02:08

I\'ve been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?

int main(int argc, char **argv)
{
    size_t size;
             


        
相关标签:
8条回答
  • 2020-11-22 02:46

    This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with -std=standard, -ansi and -pedantic options.

    0 讨论(0)
  • 2020-11-22 02:51

    You can give size to an array dynamically in if you are using Dev-Cpp compiler I have tried it and got no error but on visual c++ and visual studio compilers it is not possible. I think the reason is that dev-c++ assigns a positive number to the uninitialized int and when we give it a number it is replaced by the given one. but perhaps other compilers give null to uninitialized variables.

    0 讨论(0)
提交回复
热议问题