• C++数组长度*必须*是编译时已知的?


    目录

    一:C++数组长度必须是编译时已知?

    二:g++默认支持变长数组

    三:g++通过编译选项控制是否支持变长数组

    四:History of C++ standard

    五:其他材料


    一:C++数组长度必须是编译时已知?

    C++ Primer 5th Edition 101页提到:数组长度必须是编译时已知的。

    但,实际上,默认情况下,g++编译器支持变长数组(VLA, variable length array)。

    二:g++默认支持变长数组

    代码如下:

    1. #include<iostream>
    2. using namespace std;
    3. int main() {
    4. int a;
    5. cin >> a;
    6. int iarr[a];
    7. // a 输入 2
    8. for (int i = 0; i < a; ++i) {
    9. iarr[i] = i + 10;
    10. }
    11. // 输出为:10, 11
    12. for (int i = 0; i < a; ++i) {
    13. cout << iarr[i] << endl;
    14. }
    15. return 0;
    16. }

    编译器版本 gcc version 11.2.0,具体如下:

    1. marvin@vm:~/eden$ g++ -v
    2. Using built-in specs.
    3. COLLECT_GCC=g++
    4. COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
    5. OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
    6. OFFLOAD_TARGET_DEFAULT=1
    7. Target: x86_64-linux-gnu
    8. Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.2.0-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
    9. Thread model: posix
    10. Supported LTO compression algorithms: zlib zstd
    11. gcc version 11.2.0 (Ubuntu 11.2.0-19ubuntu1)
    12. marvin@vm:~/eden$

    编译执行都没有问题

    1. marvin@vm:~/eden$ g++ test_vla.cpp -o a
    2. marvin@vm:~/eden$ ./a
    3. 2
    4. 10
    5. 11
    6. marvin@vm:~/eden$

    三:g++通过编译选项控制是否支持变长数组

    By 2022.6.29,最新的C++ standard是C++23,C++标准依然 *不支持* 变长数组(VLA, variable length array)。注意,这里说的是C++ standard

    1. marvin@vm:~/eden$ g++ test_vla.cpp -o a
    2. marvin@vm:~/eden$
    3. marvin@vm:~/eden$ g++ -std=c++11 test_vla.cpp -o a
    4. marvin@vm:~/eden$
    5. marvin@vm:~/eden$ g++ -std=c++17 test_vla.cpp -o a
    6. marvin@vm:~/eden$
    7. marvin@vm:~/eden$ g++ -std=c++14 test_vla.cpp -o a
    8. marvin@vm:~/eden$
    9. marvin@vm:~/eden$ g++ -std=c++17 test_vla.cpp -o a
    10. marvin@vm:~/eden$
    11. marvin@vm:~/eden$ g++ -std=c++20 test_vla.cpp -o a
    12. marvin@vm:~/eden$
    13. marvin@vm:~/eden$ g++ -std=c++23 test_vla.cpp -o a
    14. marvin@vm:~/eden$
    15. marvin@vm:~/eden$ g++ -std=c++24 test_vla.cpp -o a
    16. g++: error: unrecognized command-line option ‘-std=c++24’; did you mean ‘-std=c++14’?
    17. marvin@vm:~/eden$
    18. marvin@vm:~/eden$

    C++ standard 不支持VLA,但,编译器扩展是可以支持 VLA 的,比如g++编译器就支持了 VLA,而且是默认开启的。

    1. g++默认开启 VLA 的编译器扩展功能,因此g++默认支持 VLA

    2. 使用选项 -pedantic 则 VLA 编译告警

    3. 使用选项 -pedantic-errors 则 VLA 编译失败

    1. marvin@vm:~/eden$ g++ test_vla.cpp -o a
    2. marvin@vm:~/eden$
    3. marvin@vm:~/eden$ g++ -pedantic test_vla.cpp -o a
    4. test_vla.cpp: In function ‘int main()’:
    5. test_vla.cpp:9:9: warning: ISO C++ forbids variable length array ‘iarr’ [-Wvla]
    6. 9 | int iarr[a];
    7. | ^~~~
    8. marvin@vm:~/eden$
    9. marvin@vm:~/eden$ g++ -pedantic-errors test_vla.cpp -o a
    10. test_vla.cpp: In function ‘int main()’:
    11. test_vla.cpp:9:9: error: ISO C++ forbids variable length array ‘iarr’ [-Wvla]
    12. 9 | int iarr[a];
    13. | ^~~~
    14. marvin@vm:~/eden$
    15. marvin@vm:~/eden$

    四:History of C++ standard

    Summary Table of history of various C++ versions: 

    VersionRelease DateMajor changes
    C++98 (ISO/IEC 14882:1998)October 1998The first version
    C++03 (ISO/IEC 14882:2003)February 2003Introduction of value initialization.
    C++11August 2011Introduction of Lambda Expressions, Delegating Constructors, Uniform Initialization Syntax, nullptr, Automatic Type Deduction and decltype, Rvalue References etc.
    C++14August 2014Introduction of polymorphic lambdas, digit separators, generalized lambda capture, variable templates, binary integer literals, quoted strings etc.
    C++17December 2017Introduction of fold expressions, hexadecimal floating point literals, a u8 character literal, selection statements with initializer, inline variables etc.
    C++20

    March

    2020

    This update extends C++ with the facilities to inspect program entities such as variables, enumerations, classes and their members, lambdas and their captures, etc.
    C++23Future ReleaseThe next major revision of the C++ standard

    以上图片和表格来自:History of C++ - GeeksforGeeks

    五:其他材料

    gcc对C++ standard的支持

    C++ Standards Support in GCC- GNU Project

    g++小抄

    https://bytes.usc.edu/cs104/wiki/gcc/

    g++编译常用指令_return you的博客-CSDN博客_g++ 编译命令

    GCC与gcc,g++区别

    gcc和g++的区别 - 百度文库

    GCC与gcc,g++区别_程序小人生的博客-CSDN博客_gcc与g++的区别

  • 相关阅读:
    提高 Web 开发效率的10个VS Code扩展插件,你知道吗?
    k8s笔记22--使用fluent-bit采集集群日志
    音视频技术-声反馈啸叫的产生与消除
    部分聚合平台“真自营 假聚合”?专家:扰乱市场公平秩序
    【Matlab算法】L-M法求解非线性最小二乘优化问题(附L-M法MATLAB代码)
    讯飞AI算法挑战大赛-校招简历信息完整性检测挑战赛-三等奖方案
    【CentOS 7】克隆虚拟机
    日期类完善
    浏览器本地存储之Cookie和webStorage
    【Unity | Editor强化工具】资产快速访问工具
  • 原文地址:https://blog.csdn.net/iceman1952/article/details/125514882