idk321's blog

By idk321, history, 4 years ago, In English

Take a look at the end of these two programs:

https://atcoder.jp/contests/abc188/submissions/19361247

https://atcoder.jp/contests/abc188/submissions/19361394

One is correct and the other is not, even though in theory the one with std::min should be equivalent, since the first values of the arrays are all pairwise distinct.

EDIT: std::array variable holding fundamental types of local scope is not zero initialized (unlike other containers to my knowledge).

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

»
4 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

I've initialized array in the second submission and got AC. But I have no idea why your first solution works, it passed in 3 out of 3 tries.

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Seriously? Huh, why do you think it is like that, considering the fact, that the array should at least in theory be default initialized with zeores? Oh wait, it isn't?

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      std::array declared locally goes on the stack and isn't zero-initialized (as with any locally declared primitive types).

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      std::array, unlike std::vector, does not zero-initialize its elements by default. Use std::array<T, N> var{}; to zero-initialize.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Array uses default initialization.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Oh it says so in the documentation. Thank you, I falsely assumed that like other std containers its elements are also zero initialized.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      std::vector is zero-initialized because its data is allocated on the heap (even if the vector is declared locally).

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      You should read warnings next time. Atcoder shows compilation log right below the code.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by idk321 (previous revision, new revision, compare).