c++ array - expression must have a constant value

C++Arrays

C++ Problem Overview


I get an error when I try to create an array from the variables I declared.

int row = 8;
int col= 8;
int [row][col];

Why do I get this error:

> expression must have a constant value.

C++ Solutions


Solution 1 - C++

When creating an array like that, its size must be constant. If you want a dynamically sized array, you need to allocate memory for it on the heap and you'll also need to free it with delete when you're done:

//allocate the array
int** arr = new int*[row];
for(int i = 0; i < row; i++)
    arr[i] = new int[col];

// use the array

//deallocate the array
for(int i = 0; i < row; i++)
    delete[] arr[i];
delete[] arr;

If you want a fixed size, then they must be declared const:

const int row = 8;
const int col = 8;
int arr[row][col];

Also,

int [row][col];

doesn't even provide a variable name.

Solution 2 - C++

The standard requires the array length to be a value that is computable at compile time so that the compiler is able to allocate enough space on the stack. In your case, you are trying to set the array length to a value that is unknown at compile time. Yes, i know that it seems obvious that it should be known to the compiler, but this is not the case here. The compiler cannot make any assumptions about the contents of non-constant variables. So go with:

const int row = 8;
const int col= 8;
int a[row][col];

UPD: some compilers will actually allow you to pull this off. IIRC, g++ has this feature. However, never use it because your code will become un-portable across compilers.

Solution 3 - C++

C++ doesn't allow non-constant values for the size of an array. That's just the way it was designed.

C99 allows the size of an array to be a variable, but I'm not sure it is allowed for two dimensions. Some C++ compilers (gcc) will allow this as an extension, but you may need to turn on a compiler option to allow it.

And I almost missed it - you need to declare a variable name, not just the array dimensions.

Solution 4 - C++

You can use #define as an alternative solution, which do not introduce vector and malloc, and you are still using the same syntax when defining an array.

#define row 8
#define col 8

int main()
{
int array_name[row][col];
}

Solution 5 - C++

When you declare a variable as here

int a[10][10];

you are telling the C++ compiler that you want 100 consecutive integers allocated in the program's memory at runtime. The compiler will then provide for your program to have that much memory available and all is well with the world.

If however you tell the compiler

int x = 9001;
int y = 5;
int a[x][y];

the compiler has no way of knowing how much memory you are actually going to need at run time without doing a lot of very complex analysis to track down every last place where the values of x and y changed [if any]. Rather than support such variable size arrays, C++ and C strongly suggest if not outright demand that you use malloc() to manually allocate the space you want.

TL;DR

int x = 5;
int y = 5;
int **a = malloc(x*sizeof(int*));
for(int i = 0; i < y; i++) {
    a[i] = malloc(sizeof(int*)*y);
}

a is now a 2D array of size 5x5 and will behave the same as int a[5][5]. Because you have manually allocated memory, C++ and C demand that you delete it by hand too...

for(int i = 0; i < x; i++) {
    free(a[i]); // delete the 2nd dimension array
}
free(a); // delete a itself

Solution 6 - C++

one could also use a fixed lengths vector and access it with indexing

int Lcs(string a, string b) 
{
	int x = a.size() + 1;
	int y = b.size() + 1;

	vector<vector<int>> L(x, vector<int>(y));

	for (int i = 1; i < x; i++)
	{
		for (int j = 1; j < y; j++)
		{
			L[i][j] = a[i - 1] == b[j - 1] ?
				L[i - 1][j - 1] + 1 :
				max(L[i - 1][j], L[i][j - 1]);
		}
	}

	return L[a.size()][b.size()];
}

Solution 7 - C++

Using the GCC compiler(which I did locally) and an online compiler(link), a code similar to the one in the question runs provided a variable name.

#include<iostream>

int main(){
    int r= 2, c=3;
    int arr[r][c];
    arr[0][1]= 3;
    std::cout<<arr[0][1];
}

Output: 3

Though I quote cplusplus.com,

> NOTE: The elements field within square brackets [], representing the number of elements in the array, must be a constant expression since arrays are blocks of static memory whose size must be determined at compile-time before the program runs.

It seems like the present compilers have been changed in this regard. This is a C99 standard which the maintainers of the standard compilers added to their compilers.

We need not have a const declaration. During compilation, the compiler automatically substitutes the value.

This question goes around the topic.

NOTE: This is not the standard coding practice, so some compilers might still throw an error.

Solution 8 - C++

No it doesn't need to be constant, the reason why his code above is wrong is because he needs to include a variable name before the declaration.

int row = 8;
int col= 8;
int x[row][col];

In Xcode that will compile and run without any issues, in M$ C++ compiler in .NET it won't compile, it will complain that you cannot use a non const literal to initialize array, the size needs to be known at compile time

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNicholas KongView Question on Stackoverflow
Solution 1 - C++FoggzieView Answer on Stackoverflow
Solution 2 - C++user500944View Answer on Stackoverflow
Solution 3 - C++Mark RansomView Answer on Stackoverflow
Solution 4 - C++sjtupuzhaoView Answer on Stackoverflow
Solution 5 - C++arrdemView Answer on Stackoverflow
Solution 6 - C++BigChiefView Answer on Stackoverflow
Solution 7 - C++gitarthaView Answer on Stackoverflow
Solution 8 - C++user1043000View Answer on Stackoverflow