gregr's blog

By gregr, history, 6 weeks ago, In English

I wrote the code code for the problem problem which is giving WA on test 4. I tried running it locally but it is giving me correct ans.

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

You have undefined behaviour. Use the command line arguments of g++ to detect it.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you please elaborate on same.

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I may be wrong but here they go :

      use g++ -std=c++17 -Wall -O myfilename.cpp -o myexecutable

      1. -Wall arguement to include warnings (It shows useful warnings like uninitialized/unused vars)

      2. -O to optimize your code (I don't know about this one)

      3. By default gcc names the executable as a.out(a.exe in windows) but -o is used to change the a.out to desired name replace myexecutable to anything you want. Generally name of cpp file without .cpp extention is used to name the executable with the same name as the file being compiled.

      P.S : Please correct me if I am wrong

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thanks a lot !! I will check it and get back to you. !!

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Useful is also -fsanitize=address and it's family.

        In C++ if you access an array out of bounds (A[10] if A only has length 5) or similar, it is not guaranteed to throw some kind of error, it's just undefined behavior. If you really can't access the memory, it is a segmentation fault, but if you can, it might let you access and change that.

        If you compile with -fsanitize=address and then your program does something like that, it will output a lot of crap on the screen, but it allows you to pinpoint where the error happens.

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Thank You It's a very useful information. Most of my errors are Segmentation fault or similar I would just end up retyping everything from start or worse give up.