Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should global array size be an integer constant?

In C++ I tried declaring a global array of some size. I got the error:

array bound is not an integer constant before ‘]’ token

But when I declared an array of the same type in the main() function it is working fine.

Why is there different behaviour here?

int y=5;
int arr[y];         //When I comment this line it works fine

int main()
{
    int x=5;
    int arr2[x];        // This line doesn't show any error.
}

Edit: Many are suggesting this question is a duplicate of Getting error "array bound is not an integer constant before ']' token". But that question doesn't answer why there is different behaviour.

like image 725
Syed Maqthyar Avatar asked Mar 10 '20 15:03

Syed Maqthyar


People also ask

Why global array has a larger size than the local array?

When an array is declared globally then it stores in the data segment, and the data segment has no size limit. Hence, when the array is declared of big size (i.e., more than 107) then the stack memory gets full and leads into the stack overflow error, and therefore, a segmentation fault error is encountered.

How do you make an array global in C++?

class C { int [] x; void method A(int size) { x = new int[size]; // Allocate the array for(int i = 0; i < size; i++) x[i] = i; // Initialise the elements (otherwise they contain random data) B(); delete [] x; // Don't forget to delete it when you have finished // Note strange syntax - deleting an array needs the [] } ...

What happens when an array is declared of a larger size?

Hence, when the array is declared of big size (i.e., more than 107) then the stack memory gets full and leads into the stack overflow error, and therefore, a segmentation fault error is encountered. Therefore, for declaring the array of larger size, it is good practice to declare it globally.

Why can't I const a variable in an array of arrays?

(If you don't use link-time optimization to let them inline the value.) It's simply a limitation of the language. The sizes of statically-bounded arrays need to be constant expressions, and unfortunately in C that's only something like a literal constant or a sizeof expression or such like, but not a const -typed variable.

What are the limitations of global arrays in C++?

However, the use of global arrays has some limitations. 1. These functions work only with particular array (s) and cannot be used for performing similar operations on other arrays.

How big is an array of 10^7 INTs in C?

So for your use case, you can globally define an array of 10^7 ints (~40MB), chars (~10MB) or long longs (80MB). I hope it was of some help! How do I use an array in C language with 10 million size?


2 Answers

Both examples are ill-formed in C++. If a compiler does not diagnose the latter, then it does not conform to the standard.

Why there is a different behaviour here?

You use a language extension that allows runtime length automatic arrays. But does not allow runtime length static arrays. Global arrays have static storage.

In case you are using GCC, you can ask it to conform to the standard by using the -pedantic command line option. It is a good idea to do so in order to be informed about portability problems.

like image 130
eerorika Avatar answered Oct 17 '22 03:10

eerorika


The size of an array must be a constant. You can fix this by declaring y as const.

const int y=5;
int arr[y]; 

As for why this worked in main, g++ does allow a variable length array in block scope as an extension. It is not standard C++ however.

like image 4
dbush Avatar answered Oct 17 '22 04:10

dbush