Hi everyone, I'm wondering if people who often participate in competitions, who are qualified in competitive programming, how to debug bugs? By couting everything (C++), or using the codeblock's debug tool?
# | User | Rating |
---|---|---|
1 | tourist | 3947 |
2 | jiangly | 3734 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | ecnerwala | 3581 |
8 | Geothermal | 3569 |
8 | cnnfls_csy | 3569 |
10 | ksun48 | 3479 |
# | User | Contrib. |
---|---|---|
1 | awoo | 162 |
2 | maomao90 | 160 |
3 | nor | 156 |
3 | adamant | 156 |
5 | -is-this-fft- | 155 |
5 | atcoder_official | 155 |
7 | maroonrk | 153 |
7 | cry | 153 |
9 | SecondThread | 147 |
10 | Petr | 146 |
Hi everyone, I'm wondering if people who often participate in competitions, who are qualified in competitive programming, how to debug bugs? By couting everything (C++), or using the codeblock's debug tool?
Name |
---|
cout all variables
That mostly depends on what makes you feel comfortable. Printing everything provides a bunch of information at once, without having to do every step. Using a debugger can help understand the process leading to the bug and its cause.
Most people choose to print because it's just straightforward. However, some use a debugger (even with gdb in the terminal).
There are a lot of methods for finding errors inside the written code. Here are 2 main ones:
Using a debugger. Most of the IDEs provided at the olympiads have this tool. Most of all, I personally like the GDB. It is present in CLion, Code::Blocks, DevC++. It is not difficult to understand it, but it gives great opportunities in finding errors. I advise you to watch the following video — https://www.youtube.com/watch?v=J7L2x1ATOgk. In addition, if you experience memory leaks or a segmentation error, then I advise you to use valgrind (https://valgrind.org/docs/manual/quick-start.html) (a method not for traveling olympiads).
Stress testing. A very important skill if debugging doesn't help. I found a good article on the Russian Algorithmica — https://algorithmica.org/ru/stress-test, I hope Google translator can handle it. You can also use https://cfstress.com to search for cf tests, however this service requires a subscription for full use.
I strongly do not recommend using any cout << x to check what is currently in this variable. This not only clutters up your code, but also does not give a complete understanding of what is happening in the program.