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.
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.
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 [] } ...
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.
(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.
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.
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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With