https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115869

            Bug ID: 115869
           Summary: Wrong constant evaluation with vector of struct
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at fabiangundlach dot org
  Target Milestone: ---

Created attachment 58633
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58633&action=edit
preprocessor output

The following code prints a wrong answer (6) when compiled with -O3. It prints
the correct answer (5) when compiled with -O2 or with -O3 -fno-strict-aliasing.
I don't see how I could be violating aliasing rules here. (Apologies if I'm
overlooking something obvious!)

#include <cstdio>
#include <vector>

struct st {
        int v;
        st() : v(0) {}
};

int main() {
        std::vector<st> res(3);
        res[0].v = 1;
        for (int d = 1; d < 3; d++)
                for (int i = 0; i < 2; i++)
                        for (int k = 0; k+d < 3; k++)
                                res[k+d].v += res[k].v;
        printf("%d\n", res[2].v);
        return 0;
}

Reply via email to