C++ array size dependent on function parameter causes compile errors

后端 未结 7 1798
甜味超标
甜味超标 2020-12-03 14:10

I have a simple function in which an array is declared with size depending on the parameter which is int.

    void f(int n){
        char a[n];
    };

    i         


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 14:33

    You are using something that is not a standard. Actually it is standard C but not C++. How peculiar is that!

    Explaining a bit more, run time sized stack arrays are not part of C++, but are part of C99, the latest standard for C. That´s why some compilers will get it, while others will not. I would recommend refrain from using it, to avoid compiler compatibility issues.

    The alternate implementation of the functionality would be using new and delete, as posted by strager.

提交回复
热议问题