C + + runtime determines array size Variable-length array

Source: Internet
Author: User

The variable-length array is an attribute added by C99, that is, the length of the array can be determined at runtime (run time), not at compile time (compile). That is, you do not have to use const int when you define an array, you can use variables, function return values, and so on. For example

1 int Main () {2     int a=5, b=6; 3     int Arr1[a]; 4     int arr2[min (A, b)]; 5     int arr3[rand ()%  ];       6 7 }

The way these arrays are defined is legal.

However, this kind of declarative approach seems to apply only to local variables, and global variables are not. The following usage will cause an error.

int a=; int B[a]; int Main () {    0;}

Allowing the runtime to determine the size of the array can be handy.

However, in the C11 standard, the variable-length array becomes an optional feature that does not require the compiler to implement. I tested the default support for the compilers available at hand.

Compiler Whether Variable-length array is supported
GCC 4.8 Support
Clang 3.4 Support
Visual Studio 2012 Not supported
Visual Studio 2013 Not supported

C,c++ hidden things too much, it is really troublesome. Variable-length Array is a feature that needs to be used sparingly when developing across platforms.

In addition, it is not very appropriate to see many places translated as "variable-length arrays", which are different from the edge-length arrays in other languages.

The

C + + runtime determines the size of the variable-length array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.