Why is this code not running on my compiler (TDM GCC9.2.0)?

I gave the AtCoder beginner contest 174 in which the code for Problem F fails to run when running on local compilers such as TDM GCC 9.2.0 and default GCC compiler on CodeBlocks but when I submitted the code it worked. Can someone explain what is going on because I am not able to get what is wrong here.
Solution Code
GeeksforGeeks code from which I copied.

@ssjgz @aryanc403 @vijju123
??

What precisely do you mean by “fails to run”?

2 Likes

i compiled the code using gcc 9.3.0 and clang 10.0.0 everything goes fine. can you please mention the specific error you are getting while compiling it with codeblocks or TDM GCC 9.2.0

Oh, I forgot to mention that!
Just try running the first test case provided on AtCoder.
I suppose it is some kind of runtime error because when I try the code for the test case provided it stops and returns a large negative value.
Specifically this value -1073741571

Can’t reproduce on Linux with gcc 7.5.0:

[simon@simon-laptop][16:44:45]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling n_18bcs6626-blah.cpp
n_18bcs6626-blah.cpp: In function ‘int query(int, int*, int)’:
n_18bcs6626-blah.cpp:32:35: warning: unused parameter ‘n’ [-Wunused-parameter]
 int query(int idx, int bit[], int n)
                                   ^
n_18bcs6626-blah.cpp: In function ‘int main()’:
n_18bcs6626-blah.cpp:91:9: warning: unused variable ‘l’ [-Wunused-variable]
     int l, r;
         ^
n_18bcs6626-blah.cpp:91:12: warning: unused variable ‘r’ [-Wunused-variable]
     int l, r;
            ^
Successful
[simon@simon-laptop][16:45:00]
[~/devel/hackerrank/otherpeoples]>echo "4 3
1 2 1 3
1 3
2 4
3 3" | ./a.out
2
3
1
[simon@simon-laptop][16:45:07]
[~/devel/hackerrank/otherpeoples]>


2 Likes

both of the test cases provided at Atcoders are executing fine.

ayaan@ayaan-Predator-PH315-51:~/cpp$ g++ testing.cpp     // not getting any error on compiling
ayaan@ayaan-Predator-PH315-51:~/cpp$ clang++ testing.cpp       // not getting any error on compiling
ayaan@ayaan-Predator-PH315-51:~/cpp$ ./a.out
4 3
1 2 1 3
1 3
2 4
3 3
2
3
1

// test case 1 passed

ayaan@ayaan-Predator-PH315-51:~/cpp$ ./a.out
10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8
1
2
2
1
2
2
6
3
3
3

// test case 2 passed

ayaan@ayaan-Predator-PH315-51:~/cpp$ 

other details

ayaan@ayaan-Predator-PH315-51:~/cpp$ clang++ --version
clang version 10.0.0-4ubuntu1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
ayaan@ayaan-Predator-PH315-51:~/cpp$ g++ --version
g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2 Likes

Anybody tried it on windows??
I think it is some windows specific issue, I tried it on a different a different windows machine and still the same error.

I too get a RE on windows gcc 9.2.0

Thank you for cross checking it!
But, this is something serious because I have got this error a few times earlier during contests and I have had to rewrite the whole code which is very tedious.
Is there anybody who could tell what is the real issue causing this.
Also @everule1 it would be nice if you could mention your processor and ram specs??

8gb ram + i5

Interesting. Did you step through it with a debugger to find out where it was crashing?

Edit:

Does Windows have an unusually small default stack size, or something? Can you replace this silliness:

int last_visit[MAX]; 
memset(last_visit, -1, sizeof(last_visit)); 

with this:

vector<int> last_visit(MAX, -1);

and see what happens?

1 Like

Stop copying from GFG. Most of the codes are buggy/wrong.

3 Likes

Thank you, will keep that in mind!
But that is not the issue the issue is with windows when I try declaring large arrays inside a function.

Yes, that worked!
I overlooked that the MAX was predefined and I think that it is to do something with declaring large sized arrays with predefined size.
I don’t have any idea why this happens but this does not happens when large arrays are declared during runtime.