I'm just jumping into C++, coming from C
In C (89/90), a const
is not actually a constant (as opposed to a #define
'd, enum
, or literal), but rather read-only once set. I.e, I can:
const int x = rand();
and that's fine - the point being x
isn't known until runtime. Hence, I can't
int arr[x]; // error - x is not a compile-time constant
Then, one of the C standards (99?) went ahead and allowed for variable-length arrays. Although I normally code against the ANSI standard in C, this has actually had an impact now that I am trying to pickup C++11.
As far as I know, C++ does not allow for variable-length arrays. However, many compilers allow it as an extension (GCC ?). The problem is, now that I am trying to learn C++11, I can't tell if what I'm coding is valid C++, or C++ extended with C99-compatibility. Ex:
std::default_random_engine e{};
std::uniform_int_distribution<int> d{};
const int x{d(e)};
int arr[x]; // compiles
I can't tell if this is valid C++ or not. Clearly, the value of x
is not known until runtime. I think I may not understand the difference between C and C++ const
?
In C++ the size of the character constants is char. In C the type of character constant is integer (int). So in C the sizeof('a') is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.
Arrays in C/C++ It is a group of variables of similar data types referred to by a single element. Its elements are stored in a contiguous memory location. The size of the array should be mentioned while declaring it.
There is no preferred one, its based on your choice of usage for that array or object. You have to understand mutation and reassigning clearly. If your usecase only needs mutation, you can go for const.. if you need reassigning then go for let.
You can declare one-dimensional (1D) arrays with any non-negative size. int [] arr = new int[ 10 ]; // Array of size 10 int [] arr2 = new int[ 100 ]; // Array of size 100 int [] arr3 = new int[ 1 ]; // Array of size 1 int [] arr4 = new int[ 0 ]; // Array of size 0!
You are correct VLAs are a C99 feature(optional in C11) and the C++ standard does not include this feature although both gcc
and clang
allow them in C++ as an extension. We can see they are not allowed by going to the draft C++11 standard section 8.3.4
Arrays which says:
D1 [ constant-expressionopt] attribute-specifier-seqopt
^^^^^^^^^^^^^^^^^^^^^^
For both gcc
and clang
using the -pedantic
flag will warn when you are using an extension. If you are targeting C++11 then you should also specify that using -std=c++11
. You can use -pedantic-errors
to turn the warning into errors. If you compile your code using -pedantic
you should see the the following warning:
warning: ISO C++ forbids variable length array 'arr' [-Wvla]
int arr[x]; // compiles
^
gcc
documents their support for various standards, defaults and flags to enforce standard on their Language Standards Supported by GCC page and it says:
to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).
In general clang
supports what gcc
does but you can find more details on their Language Compatibility page.
Note as mentioned by GingerPlusPlus std:vector is considered the alternative for VLA in C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With