I was practicing a question PALIN3, here is the link-> https://www.codechef.com/problems/PALIN3
I coded my logic and here it is-> https://www.codechef.com/viewsolution/18880436
Now when i run this code for the sample test case provided on the Codechef IDE, it sometimes displays 4 as output and sometimes 5 without any changes in the code.
So something is wrong with my code or IDE??
For anyone who is going to test my code, i executed it contiously 5-6 times and observed this.
And since i have got a WA, it would be really helpful if you can tell my mistake. its a bit different from editorial but i think it should work.
ll sum[s.length()]={0};
//More ccode
ll p[n]={0};
ll q[n]={0};
How did this even compile?! xD
I feel you are suffering from runtime error leading to undefined behaviour. Try checking any line like-
c=sum[start+len-1]-sum[start-1];
which may lead to accessing index -1 etc.
1 Like
Yeah. Once again the evil of the by-default-enabled VLA extension of g++. Please don’t do stuff like this folks.
And you’re correct with
c=sum[start+len-1]-sum[start-1];
being problematic. For the example input the first time this line is run it tries to access sum[-1] twice - which is very much undefined behavior.
1 Like
@vijju123 i understood the problem thanks!!
But i wanted to ask why were you expecting compilation error,i mean i always declare arrays like that…is it a problem??
If that N is a constant, its fine. But if its a variable, then such initialization isnt allowed on some versions afaik. Some further say that variable sized arrays cannot be declared.
Usually I feel its a good practice to intialize them with a for loop if declaring like that.
Thanks for backup @schleppel
1 Like