Dynamic array in Stack?

前端 未结 6 670
眼角桃花
眼角桃花 2020-11-27 21:25

Is this correct ? This is compiled with g++ (3.4) sucessfully.

int main()
{
    int x = 12;
    char pz[x]; 
}
6条回答
  •  春和景丽
    2020-11-27 22:10

    G++ supports a C99 feature that allows dynamically sized arrays. It is not standard C++. G++ has the -ansi option that turns off some features that aren't in C++, but this isn't one of them. To make G++ reject that code, use the -pedantic option:

    $ g++ -pedantic junk.cpp
    junk.cpp: In function ‘int main()’:
    junk.cpp:4: error: ISO C++ forbids variable-size array ‘pz’
    

提交回复
热议问题