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

Автор Xiaohuba, история, 6 часов назад, По-английски

[Apologize for my poor English.]

The code at https://godbolt.org/z/PP1TTdhxf outputs 1. However, if you simulate the code, the answer is obviously 20.

Further exploration showed that the compiler compiled function qpw, but did not call it. Uncomment line 22 solves the issue, surprisingly. Further more, the bug seems to only occur on gcc12.1 and gcc12.2, with -O2 enabled.

I believe the code does not contain any undefined / unspecified behavior.

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

»
6 часов назад, # |
Rev. 2   Проголосовать: нравится -12 Проголосовать: не нравится

It was because when O2 enabled it does run faster however every variable you create if you don't have a value for it, it will be undefined instead of 0 like default. So therefore, you must define every variables before working on it. (So uncomment line 22 will actually solve the issue)

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

    Please read the code carefully. On line 9, I cleared up every variable in mat, and since it's a constructor, no undefined value is envolved during calculation.

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

      oh yeah I'm sorry about that, and I just find out that if you add cout<<b.mat[0][0]<<" "<<b.mat[0][1]<<" "<<b.mat[1][0]<<" "<<b.mat[1][1]<<"\n"; in your operator * (Matrix b) you will get 20

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

        Because it also adds a side affect, and avoids wrong optimization.

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

    Uncommenting line 22 makes a side affect, and avoids the compiler to (wrongly) optimize out the function call.

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

It seems like the compiler used qpw function as a constexpr one to optimize the call out, but it is not marked as constexpr. Very strange, maybe it is a bug indeed.

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

I added an operator= function which works just fine. https://godbolt.org/z/vjb4vdczc