Does C++11 support variable sized arrays? [duplicate] - c++

This question already has answers here:
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 2 years ago.
I'm very new to C++, but I've been digging around and the general answer is no, c++11 does not support variable sized arrays, since array sizes need to be constant expressions.
However, I've tried this code on XCode 11 (C++11):
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
unsigned long arrayS;
cin >> arrayS;
bool a[arrayS];
return 0;
}
and it totally works. When I set a breakpoint at
bool a[arrayS];
I can see that the array a has arrayS elements. I have verified this with
*(&a + 1) - a
and it shows that the number of elements in a is arrayS.
Does c++11 support variable sized arrays? Or, is it only working for me because of the compiler I'm using?
I'm asking this question because I'm unsure of what compiler my friend is using and want to send the code to him to run.
Any help is much appreciated.

That is non standard C++. Some compilers accept it, but if you want to be portable you should stick with standard C++.

In c++, array size should be supplied at compile time. For variable sized arrays use std::vector

Related

Visual Studio Community 2019 requires a constant value inside square brackets [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 2 years ago.
I've been trying to find answers for this lately, but I just can't seem to understand why the compilers for C++ that Microsoft has been using can't compile such a code :
#include<iostream>
int main()
{
int n;
std::cin >> n;
int x[n];
}
It gives those errors :
However, this code compiles on a lot of different compilers.
Could someone point me to somewhere, I couldn't find any answers for this.
See How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)? for discussion on the matter. Briefly, C++ requires that raw arrays have a constexpr for their size, so they it knows how much memory to allocate for it at the moment it's declared instead of at runtime after receiving user input. Some compilers I suppose are more permissive on this.
Variable length arrays are not standard C++. Some compilers provide them as a language extension but they are not portable.
This code shouldn't compile.
In C++, arrays are statically allocated and their required memory must be specified at compile time.
Hence, a user input is not a suitable value for array declaration. If you want a dynamic array, you can try using malloc like this:
#include<iostream>
int main()
{
int n;
std::cin >> n;
int* x = (int*)malloc(n*sizeof(int));
}
This way, the array is actually stored in heap, and can have any arbitrary size, changing over lifetime of the program.
Another alternative is std::vector of course.

Using variable to initialize an array in C++ [duplicate]

This question already has answers here:
In C++ books, array bound must be constant expression, but why the following code works?
(2 answers)
Closed 5 years ago.
As C++ primer said, we can't use variable as the dimension of builtin array, so the following code did not work
int length = 3;
int array[length] = {0, 1, 2};
the error is
error: variable-sized object may not be initialized
But why following code works?
int length = 3;
int array[length];
This is an extension by your compiler called a Variable Length Array (VLA) and is not in the C++ standard which means that this code can break at any moment you switch compilers or the compiler vendor decides to no longer support this feature. If you want a variable length array that does not depend on this extension but instead on the standard you should use a std::vector.

Array Size Declaration issue [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 8 years ago.
int n=5;
int arr[n];
I want to declare size of array as above in C++, but I get error while compiling. I find a lot of code in internet which uses these type of declaration instead of simple putting int arr[5]. How come the code compiles successfully for them but not for me. P.S: I use windows7 and Visual Studio(IDE).
Error Message : Expresion must have a constant value
The number of elements of the array, the array bound, must be a constant expression.
You have to use
const int n = 5;
or
constexpr int n = 5;
else it is a non standard extension : variable length array (VLA).
The error message actually describes rather well what’s going on: C++ does not support arrays with a non-constant size (more precisely, the size needs to be known at compile time).
There are two solutions for this:
If the size is actually a constant, declare it as constexpr (if you can’t use C++11, you can also use const):
constexpr int n = 5;
std::array<int, n> arr;
Which requires the standard header <array>. Or, if you cannot use C++11, change the second line to
int arr[n];
If the size isn’t known at compile time, don’t use a static array, use a dynamic container instead:
int n = 5;
std::vector<int> arr(n);
This requires the <vector> standard header.

Some way for allocating and initializing array faster than plain loop-and-set [duplicate]

This question already has answers here:
Initialization of all elements of an array to one default value in C++?
(12 answers)
Closed 8 years ago.
This is how I allocate and initialize an array in C++.
char *dataPtr;
dataPtr= new char [40000];
int i;
for(i=0;i<40000;i++)
{
dataPtr[i]=200;
}
Is there any other faster and cleaner way of achieving this as the for loop is taking considerable time for my application?
Use an std::vector:
std::vector<char> data(40000, 200);
You can use memset(), which has a core of optimized assembly with SSE/SSE2/etc instructions as appropriate at runtime. The memset() function is designed to do exactly this task: set each char in an array of char to a specific value, as quickly as possible.
#include <cstring>
char *dataPtr = new char[LENGTH];
std::memset(dataPtr, 200, LENGTH);
However, modern compilers will do this for you, you can check the assembly, and you might find a call to memset() or something similar in the original code that used a for loop. It's only 40 kB anyway, so you're not going to save much time, unless you have to initialize the array very often.
To initialize each array elements by 200 you can use
std::fill_n(dataPtr, 40000, 200);

Array of non-constant size: Why does this even work? [duplicate]

This question already has answers here:
C++: Why does int array[size] work?
(3 answers)
Closed 8 years ago.
#include <iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the size :";
cin>>n;
int array[n]; // I've worked some outputs and it works
return 0;
}
Is this some kind of dynamic allocation?
Why doesn't it even gives an error for 'n' to be a "const"?
Also, writing cout << array[n+5]; doesn't result in an compile time or runtime error.
I'm using Dev-C++.
Apparently one can declare variable length arrays in C99, and it seems GCC accepts then for C++ also.
Variable-length automatic arrays are allowed in ISO C99, and as an
extension GCC accepts them in C90 mode and in C++. These arrays are
declared like any other automatic arrays, but with a length that is
not a constant expression.
You learn something every day .. I hadn't seen that before.