Help needed in Doctor Chef (WA in sample testcases)

what is this flag ? i have no idea what is this and from where to learn to use this. from where can i learn about this ? will that codeforces link guide me properly ?

1 Like

SIGABRT error is being returned in output console when i use
#pragma GCC optimize “trapv” on teh top of my code

with this flag i m getting something like this but i cannot figure out why ?

prog.cpp: In function ‘std::basic_ostream<_CharT, _Traits>& std::flush(std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’:
prog.cpp:150:1: internal compiler error: Segmentation fault
 }
 ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-6/README.Bugs> for instructions.

plz help me anyone

can you plz help ?? i don’t how to use these flags. PLz respond

There’s an example of using them here.

If you’re getting an internal compiler error, then there’s not much I can recommend except “upgrade your compiler” (GCC 6 is pretty old).

1 Like

can’t i do this on codechef’s or codeforces’s compiler ?

You can, but you’ll get much less useful information about what’s going on, and won’t be able to use a debugger. I wouldn’t bother, personally: just sort out your local build environment and use that.

1 Like

Can you share some syntax on how to use these flags in a c++ code ?

And also if possible a small example of code along with with the output error message, so that i can learn how to use this.
Also i don’t know anything about pragma.

Passing a bunch of debug flags to g++:

[simon@simon-laptop][13:36:04]
[~/devel/hackerrank/otherpeoples]>g++ rudyiscrazy1-DRCHEF.cpp  -g3 -O3 -Wall -Wextra  -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
rudyiscrazy1-DRCHEF.cpp: In function ‘int main()’:
rudyiscrazy1-DRCHEF.cpp:41:14: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&N) ;
         ~~~~~^~~~~~~~~
rudyiscrazy1-DRCHEF.cpp:42:15: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%ld",&x) ;
         ~~~~~~^~~~~~~~~~
rudyiscrazy1-DRCHEF.cpp:53:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
             scanf ("%ld",&val) ;
             ~~~~~~^~~~~~~~~~~~
[simon@simon-laptop][13:36:12]
[~/devel/hackerrank/otherpeoples]>echo "3
> 5 5
> 1 2 3 4 5
> 5 1
> 40 30 20 10 50
> 3 10
> 20 1 110" | ./a.out
rudyiscrazy1-DRCHEF.cpp:122:41: runtime error: signed integer overflow: -6148914691236495360 * 2 cannot be represented in type 'long int'
rudyiscrazy1-DRCHEF.cpp:14:27: runtime error: signed integer overflow: 5764607523034234880 * 2 cannot be represented in type 'long int'
/usr/include/c++/7/debug/safe_iterator.h:327:
Error: attempt to decrement a dereferenceable (start-of-sequence) iterator.

Objects involved in the operation:
    iterator "this" @ 0x0x7ffcb476d060 {
      type = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<long>, std::__debug::multiset<long, std::less<long>, std::allocator<long> > > (mutable iterator);
      state = dereferenceable (start-of-sequence);
      references sequence with type 'std::__debug::multiset<long, std::less<long>, std::allocator<long> >' @ 0x0x7ffcb476d300
    }
Aborted (core dumped)

If you can’t do that, you can make sure the first two lines in your .cpp file are:

#define _GLIBCXX_DEBUG       // Iterator safety; out-of-bounds access for Containers, etc.
#pragma GCC optimize "trapv" // abort() on (signed) integer overflow.

(this doesn’t include the -fsanitize=undefined flag, alas - not sure how to do that), but as I mentioned, this seems to give much less useful information, so I’d stick with compiling locally and passing the flags to gcc.

1 Like

hey i got few things cleared from your local compiling ! so there are two places where i am getting int overflow and at one place i m decrementing a iterator pointing to start of the container.

but there is new problem eating my head -
#pragma GCC optimize “trapv”

does this give an error message similar to the fashion in which we get when we use #define _GLIBCXX_DEBUG

or it just tells us a SIGBART error

herre is what i get when i compiled my code with just #pragma GCC optimize “trapv”

That just gives SIGABRT on signed integer overlow. It’s mainly useful for deciding whether your WA is due to integer overflow or not; -fsanitize=undefined is probably better for local debugging.

1 Like

okay things are getting clearer, Thnx a lot !
By the way, what does this part means, " ignoring return value of ‘int scanf(const char*, …)’, declared with attribute warn_unused_result [-Wunused-result] ", while you are compiling ??

scanf has a return value, but you are ignoring it (as do about 99% of C programmers in the world :))

2 Likes

so i started using VS code from today and set it up for debugging from
this official page : Get Started with C++ and Mingw-w64 in Visual Studio Code

and when i tried to locally compile a small code to check whether the flags are working i got this :

can you help me figure it out why ??

No, sorry - I don’t use Windows.

1 Like

after setting up local compiling trough my windows cmd terminal and g++ compiler my compiling failed when i used this flag -fsanitize=undefined

when i used this command :
C:\Users\Humble Fool\Desktop\dummy>g++ main.cpp -g -D_GLIBCXX_DEBUG -ftrapv
the .cpp file compiled properly !

but when i used this command :
C:\Users\Humble Fool\Desktop\dummy>g++ main.cpp -g -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv

I got this error -

C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lubsan        
collect2.exe: error: ld returned 1 exit status

few people on stackoverflow were saying that maybe my compiler doesn’t supports it ! but if that’s the case then how is it working on your machine ?

It’s not working because the linker can’t find libubsan.so. Maybe you should shift to MSVC because it has support for AddressSanitizer. Hope this helps. :slight_smile:

in case Windows annoys you too much, Linux welcomes you :stuck_out_tongue_winking_eye:

1 Like

what is MSVC ??

@ssjgz can you give me some links to resources where i can learn how to setup my machine for being able to debug c++ code just like you did ??