Array[n] vs Array[10] - Initializing array with variable vs numeric literal
In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic
about following the C++ standard), you can do:
int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++
If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:
int n = 10;
double* a = new double[n]; // Don't forget to delete [] a; when you're done!
Or, better yet, use a standard container:
int n = 10;
std::vector<double> a(n); // Don't forget to #include <vector>
If you still want a proper array, you can use a constant, not a variable, when creating it:
const int n = 10;
double a[n]; // now valid, since n isn't a variable (it's a compile time constant)
Similarly, if you want to get the size from a function in C++11, you can use a constexpr
:
constexpr int n()
{
return 10;
}
double a[n()]; // n() is a compile time constant expression
define array with unsure size
The correct terminology is variable-length-array (VLA).
The C++ language standard does not support this feature.
The C language standard started supporting it at some point.
Allocation in memory is compiler-dependent (i.e., not dictated by the standard).
Defining the amount of elements in an array
The variable vector
is a Variable-Length Array (VLA). Standard C++ doesn't support that, but some compiler supports that as extension.
The variable TOPE
is not initialized at the point where the variable vector
is declared, so its size is indeterminate.
To set the size of VLA to the number entered, you should write like this:
int TOPE;
cin >> TOPE ;
int vector[TOPE] ;
To do it in more standard way, you should use std::vector
instead of VLA:
int TOPE;
cin >> TOPE ;
std::vector<int> vector(TOPE);
Why does this c++ program compile?
In C++, variable length arrays are not legal. G++ allows this as an
"extension" (because C allows it), so in G++ you can do this.
Check this answer / answer further.
Does C++11 support variable sized arrays?
That is non standard C++. Some compilers accept it, but if you want to be portable you should stick with standard C++.
why can't I initialize the array like this?
This statement
int array[a]={0};
declares a Variable Length Array (VLA).
According to C Standard (6.7.9 Initialization)
3 The type of the entity to be initialized shall be an array of
unknown size or a complete object type that is not a variable length
array type.
The problem is that the compiler shall know the array size at compile time that to generate the code that initialize an array.
Consider an example
void f( size_t n )
{
int a[n] = { 1, 2, 3, 4, 5 };
//...
}
Here is a is a variable length array. Now as n can have any value then the number of initializers in the array definition can be greater than the size of the array. So this code breaks the Standard from another side because the number of initializers may not be greater than the number of elements of array. On the other hand if the number of initializers less than the number of elements of array then what to do in this case? Maybe the programmer did not mean that some elements shall be zero-initialized.
As for this declaration
int array[5]={0};
then there is no variable length array. The size of the array is known at compile time. So there is no problem
.
Initialized a pointer array in C - Variable sized object may not be initialized
Variable length arrays may not be initialized in their declarations.
You can use the standard string function memset
to initialize the memory occupied by a variable length array.
For example
#include <string.h>
//...
int c = 15;
Struct *Pointer[c];
memset( Pointer, 0, c * sizeof( *Pointer ) );
Pay attention to that variable length arrays shall have automatic storage duration that is they may be declared in a function and may not have the storage specifier static
.
How could I solve this error: expression must have a constant value. environment is VS 2013
GLfloat light_specular[light_data->light_num][4];
This is what's called a Variable Length Array. It means what it sounds like it means. One or more of the dimensions, first one in this case, is a variable size. They aren't allowed by the C++ standard. Some C++ implementations, notably g++, allow VLA syntax but many implementations, notably Visual Studio, do not.
Probably the simplest and best standard-supported solution is to do something like
std::vector<std::array<4, GLFloat>> light_specular(light_data->light_num);
and get dynamic sizing and scoped memory management on your side while maintaining data contiguity and resulting cache friendliness.
Docs on std::vector
Docs on std::array
Related Topics
Variable Length Array (Vla) in C++ Compilers
Wchars, Encodings, Standards and Portability
What Is Iaca and How to Use It
Stringification - How Does It Work
Testing Pointers For Validity (C/C++)
How Well Is Unicode Supported in C++11
Forward Declaring an Enum in C++
Correct Way to Work With Vector of Arrays
How to Pass a Unique_Ptr Argument to a Constructor or a Function
How to Serialize an Object in C++
Is a String Literal in С++ Created in Static Memory
What Are All the Member-Functions Created by Compiler For a Class? Does That Happen All the Time
Two Phase Lookup - Explanation Needed
Initializer Lists and Rhs of Operators
How to Declare Two Variables of Different Types in a For Loop