The program ran properly on my computer when i used arr[10^6], but when i submitted it to the online
judge i got sigsegv, i changed arr[10^6] to arr[1000000] and the program got accepted. I dont understand why this happened? Why does the compiler treat arr[10^6] as sigsegv?
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.
Reference: http://www.cplusplus.com/
Check here for more information on arrays
I have also mentioned this point in this discussion, which also discusses about the complexity of the solution that will be accepted without time limit exceed(tle).
And this might be the reason too : as 10 ^ 6 is 12 (bit wise XOR operator) i.e every time you are declaring arr[12] only 12 elements,and now when you try to get arr[i] ,i>12 you may get sigsegv
Here’s something, I studied it in my c++ book in 11 grade, author SUMITA ARORA
-
An integer constant must have one digit, no decimal point, may contain a + or - sign. A number with no sign is positive and commas cannot appear in an integer constant.
-
A floating constant has its own rules for formation.
The thing to note is that floating constant can be expressed as an exponent and also in real form, and an integer constant cannot be expressed as an exponent.
An array size inside the square brackets[] must strictly be an integer constant and 10^6 not being one, gives Run time error.
this is the correct reason
@achaitanyasai’s explaination about the interpretation of 10^6 looks correct(good answer), I simply misunderstood the interpretation of ^(bitwise xor operator).
Here’s the detailed explaination of @achaitanyasai’s answer,
10 in binary is 1010 and 6 in binary is 0110
1010^0110=1100, which is binary of 12(an integer constant).
Note: 1^0=1, 0^1=1, 0^0=0, 1^1=0
but remember, all the information in my comment about the rules of constants and the fact that the value inside braces of an array, representing no. of elements, must be an integer constant, is a correct rule with arrays.
Here are a few points to ponder,
- You want to express 1000000 in exponential form
the way to do that is 1.0E06.
2. Numbers in exponential form are of float/double type.
-
^ is the bitwise xor operator.
-
If array size is exponential, ie, a[1.0E06], it is a compilation error.
Here’s code link-
[1]
error link-[error][2]
[1]: http://www.codechef.com/viewsolution/3968476
[2]: http://www.codechef.com/view/error/3968476