Array size at run time without dynamic allocation is allowed?

前端 未结 8 1691
一个人的身影
一个人的身影 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:24

    This is valid in C99.

    C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.

    Note that this is different from malloc and new. gcc allocates the array on the stack, just like it does with int array[100] by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca.

提交回复
热议问题