This question already has answers here:
Declaring an array of negative length
(3 answers)
Closed 8 years ago.
#include <iostream>
using namespace std;
int main()
{
cout<<"started "<<endl;
int n= -2;
int array[n];
array[0]=100;
array[1]=200;
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<"over"<<endl;
return 0;
}
Why does this compile and run? I expected a compilation error because value of n is negative.
Variable-length arrays are not a C++ feature, so this line will cause a compilation error in a compiler that is ordered to strictly adhere to the C++ specification, regardless of the value of n (unless n is const and can be determined at compile-time):
int array[n];
Likely you are using a compiler that supports variable-length arrays as an extension. Therefore, the rules regarding what is valid depend entirely on the specific compiler and compiler options, and any code that you write using this extension will not be portable to compilers that don't support this non-standard feature.
Related
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.
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
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 4 years ago.
As I know in c++ if you want to crete an array, you must give constant value for its size. But here:
int main(){
int a;
cin >> a;
int b[a] = {};
for (int i = 0; i<a ; i++){
b[i] = a;
cout << b[i];
}
return 0;
}
If i input 5
output:
55555
It works fine in a way i can't understand in dev c++. If i run this in visual studio 2017, it gives error. Can anyone explanin why?
Are you using GCC by any chance? This is a GCC extension and it's enable by default. In fact it's quite a dangerous one because it's fairly easy to cause a stack overflow on your program. It's roughly the same as using alloca().
In order to disable it, you should use a compiler flag called -Wpedantic. This will make your compiler issue a warning. (see this demonstration)
ISO C++ forbids variable length array ‘b’ [-Werror=vla]
As I know in c++ if you want to crete an array, you must give constant value for its size.
Correct. If you use a non-constant value, then the program is ill-formed. Yes, the program that you show is ill-formed.
It works fine in a way i can't understand ... Can anyone explanin why?
C++ compiler may allow compilation of an ill-formed program. This enables the compilers to extend the language. It appears that you were using a non-standard extension to C++.
This is what the GCC compiler says about your program:
warning: ISO C++ forbids variable length array 'b' [-Wvla]
int b[a] = {};
^
This question already has answers here:
Array size at run time without dynamic allocation is allowed? [duplicate]
(8 answers)
Closed 6 years ago.
I am reading C++ Primer plus on arrays, and it says the following
typeName arrayName[arraySize];
//Arraysize cannot be a variable whose value is set while the program is running"
However, I wrote a program
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int array[n];
for(int i=0; i<n; i++)
{
cout<<array[i]<<endl;
}
}
And it works fine, I am able to set the size of the array during run time. I am not getting any compilation errors, or run time crashes.
Can someone explain what is happening?
Thanks
Some compilers like g++ allow the use of C variable length arrays and will happily compile the code without any warnings or error. This is not standard and is a compiler extension.
If you need an "array" and you do not know what the size will be until run time then I suggest you use a std::vector You can use it as a direct replacement to an array but it allows run time sizing and it offers a lot of other useful features.
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.