Блог пользователя Bhj2001

Автор Bhj2001, история, 5 лет назад, По-английски
 #pragma GCC optimize ("trapv") // kills the program on integer overflows (but is really slow).

This helps a lot in testing. If a solution fails on a particular test case this can help in finding the bug.
This works on my computer but does not work on Codeforces.
Is there an alternative to this on online judges?

  • Проголосовать: нравится
  • +38
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +23 Проголосовать: не нравится

This is very useful option indeed, just remember, that it does not work with unsigned integers and with casts

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    What do you mean by "does not work with casts"? Does it mean that casts donot happen at all when using this.

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +17 Проголосовать: не нравится

I compile with these flags and one of them turns on checking for overflows (but slows the program down too). This won't likely help you when submitting to an online judge :(

g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +17 Проголосовать: не нравится

On Codeforces, you can submit with the compiler "Clang++ 17 Diagnostics", which have much more checking than just integer overflow error. However, because test cases on Codeforces are large, you might get time limit exceeded before getting to the desired test case.


See also: Catching silly mistakes with GCC — Codeforces (including tips on how to check integer overflow, array out of bound error, check for precondition in STL algorithm, turn on warnings, etc.)

»
4 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Hi, i used this pragma in my code to detect the overflow in long long int type but it is not working.. this is my code:

void overflow() {
  long long int max = numeric_limits<long long int>::max();
  long long int min = numeric_limits<long long int>::min();
  max += max;
  min -= min;
  cout << max << ":" << min;
}

is there any additional compiler flags that i have to use to detect these bugs? thanks

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +26 Проголосовать: не нравится

    Sorry for a posting this after 3 years, but I know a one of the all possible solutions and want to share it. You can disable all optimizations and then you can enable trapv:

    #pragma GCC optimize("O0,trapv")
    
    Testing in a custom invocation

    Bhj2001, I hope, that it resolves your initial problem too.

»
22 месяца назад, # |
  Проголосовать: нравится -50 Проголосовать: не нравится

Fuck this thing