farmersrice's blog

By farmersrice, history, 6 months ago, In English,

http://codeforces.com/contest/963/submission/37451734

Not sure where my code has a bug, but I submitted 3 times and it's always the same error on test 48.

I could not decipher this part of the message:

Diagnostics detected issues [cpp.clang++-diagnose]: =================================================================
==6068==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x14e00978 at pc 0x00dc63fd bp 0x1355f754 sp 0x1355f750
READ of size 4 at 0x14e00978 thread T0

I would appreciate any help. Thanks!

 
 
 
 
  • Vote: I like it  
  • +5
  • Vote: I do not like it  

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Hmm, I fixed some possible string out of bounds issues but it's still failing

http://codeforces.com/contest/963/submission/37452849

»
6 months ago, # |
  Vote: I like it +16 Vote: I do not like it

Is your address 6969 or something that you need Address Sanitizer

»
6 months ago, # |
  Vote: I like it +10 Vote: I do not like it

Subtracting k from possible.size() can do bad things, since possible.size() is an unsigned int.

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

    It worked! Thanks!

    • »
      »
      »
      2 weeks ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      "#define SZ(x) ((int)(x).size())" and you're good to go never needing to care about these stupid unsigned sizes again

»
6 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

37476557

Here is the Problem

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

    Local arrays with variable length are illegal in C++.

    int n = 5;
    char c[n]; // Compilation error
    
    const int n = 5;
    char c[n]; // OK
    

    But in G++11 it's sometimes correct on small lengths. Two key words there are sometimes and small. I can't surely say is it UB or not, but it's surely wrong C++ code.

    Three options: 1) Use global array with big enought const length;
    2) Use stl-vector;
    3) Use new-statement to construct array.

    • »
      »
      »
      2 weeks ago, # ^ |
        Vote: I like it +26 Vote: I do not like it

      Variable length arrays are an extension supported by GCC.

      I'd like to point out a possible misunderstanding of what a language extension is. Compiler authors don't just go like "here's an extension, and it sometimes works, but sometimes doesn't so take care". If it's supported by the compiler, it should work as documented for the intended uses, same as all other language features do.

      Also, the name of the extension may be confusing. Actually, the memory is allocated at the point of declaration. If we change the vaule of n afterwards, the array itself does not change in any way.

      • »
        »
        »
        »
        2 weeks ago, # ^ |
          Vote: I like it +15 Vote: I do not like it

        Thanks for GCC extensions, now I know something new. Though I still stick with standartized C++.

        But what ASan complains about if it is not non-const length array?