g++ 9.1 更新后 std::stable_sort 出现问题

Fel*_*lli 5 c++ g++ std c++17

从 gcc/g++ 8.1更新到9.1并重新编译我的代码后,它的大部分测试都失败了。因此,进行了一些挖掘,我发现这std::stable_sort就是问题所在。

事实证明,我调用的大多数调用std::stable_sort都是不必要的,也就是说,调用std::sort就足够了。因此,我在可能的情况下进行了替换,并且关于这些代码段的测试再次成功。

现在,我只有一个电话 std::stable_sort

void MshReader::determinePhysicalEntitiesRange() {
    // conns is not empty

    std::stable_sort(this->conns.begin(), this->conns.end(), 
        [=](const auto& a, const auto& b){
            return a[this->index] < b[this->index];
        }
    );

    // acess some values of conns
}
Run Code Online (Sandbox Code Playgroud)

其中connsstd::vector<std::vector<int>>存储元素连接性的。排序是基于列index完成的,它的值在类头中分配,并且所有std::vector<int>conns 中都有该条目。

另一个值得一提的事实是,在调试版本中(使用编译器标志“-g”,不使用“-O3”)所有测试都成功

此外,在发布版本(使用标志“-O3”,不使用“-g”)时,通过在调用 之前和之后打印conns的值std::stable_sort,我发现conns被破坏了。

row:
  0:    0   2   0   1
  1:    0   2   1   2
  2:    0   1   2   5
  3:    0   1   5   8
  4:    0   3   8   7
  5:    0   3   7   6
  6:    0   0   6   3
  7:    0   0   3   0
  8:    1   4   3   4   9
  9:    1   4   3   9   6
 10:    1   4   4   7   9
 11:    1   4   6   9   7
 12:    1   4   1   2  10
 13:    1   4   1  10   4
 14:    1   4   2   5  10
 15:    1   4   4  10   5
 16:    2   4   4   5   8   7
 17:    2   4   0   1   4   3
Run Code Online (Sandbox Code Playgroud)

row:
  0:    0   0   6   3
  1:    0   0   3   0
  2:    0   1   2   5
  3:    0   1   5   8
  4:    0   2   1   2 // there were two rows with column 'index' = 2
  5:    0   3   8   7
  6:    0   3   7   6
  7:    1   4   2   5  10  10  10  10 // this entry was previously on row 14; extra '10's
  8:    1   4   3   4   9
  9:    1   4   3   9   6
 10:    1   4   4   7   9
 11:    1   4   6   9   7
 12:    1   4   1   2  10
 13:    1   4   1  10   4
 14:    1   4   2   5  10
 15:    1   4   4  10   5
 16:    2   4   4   5   8   7
 17:    2   4   0   1   4   3
Run Code Online (Sandbox Code Playgroud)

调试版本中,std::stable_sort输出预期结果。除此之外,c++17正在使用(编译器标志“-std=c++17”)。

所以,

  • 我的电话有问题std::stable_sort吗?

  • g++ 的哪些变化导致了这种行为?

  • 为什么这种行为只出现在发布版本中?

最小示例

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

template<typename InputIt>
void print2D(InputIt cbegin, InputIt cend, std::string&& message) {
    std::cout << message;
    for (auto i = cbegin; i != cend; ++i) {
        for (auto j = i->cbegin(); j != i->cend(); ++j) {
            std::cout << "\t" << std::setw(3) << std::right << *j;
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

int main() {
    int index = 1;
    std::vector<std::vector<int>> conns{{0,2,0,1},{0,2,1,2},{0,1,2,5},{0,1,5,8},{0,3,8,7},{0,3,7,6},{0,0,6,3},{0,0,3,0},{1,4,3,4,9},{1,4,3,9,6},{1,4,4,7,9},{1,4,6,9,7},{1,4,1,2,10},{1,4,1,10,4},{1,4,2,5,10},{1,4,4,10,5},{2,4,4,5,8,7},{2,4,0,1,4,3}};

    print2D(conns.cbegin(), conns.cend(), "\n\n\tbefore\n");

    std::stable_sort(conns.begin(), conns.end(), 
        [=](const auto& a, const auto& b){
            return a[index] < b[index];
        }
    );

    print2D(conns.cbegin(), conns.cend(), "\n\n\tafter\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果以上编译

    g++ -o main main.cpp -m64 -std=c++17 -O3
Run Code Online (Sandbox Code Playgroud)

它输出segmentation fault (core dumped). 然而,如果不使用标志“-O3”,则获得预期结果。

Ted*_*gmo 2

当用 g++ 9.1.1 编译时,第一个“ b”被传递到 lambda 中,size()==0这应该是不可能的。clang++ 8.0.0 使用相同的编译标志运行它没有问题,并且在运行时不会显示任何奇怪的东西valgrind

我想说这是 g++ 中的一个错误。lambda 函数中带有调试输出的代码:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

template<typename InputIt>
void print2D(InputIt cbegin, InputIt cend, const std::string&& message) {
    std::cout << message;
    for(auto i = cbegin; i != cend; ++i) {
        for(auto j : *i) {
            std::cout << "\t" << std::setw(3) << std::right << j;
        }
        std::cout << "\n";
    }
    std::cout << "\n";
}

int main() {
    size_t index = 1;
    std::vector<std::vector<int>> conns{
        {0, 2, 0, 1},       {0, 2, 1, 2},      {0, 1, 2, 5},     {0, 1, 5, 8},
        {0, 3, 8, 7},       {0, 3, 7, 6},      {0, 0, 6, 3},     {0, 0, 3, 0},
        {1, 4, 3, 4, 9},    {1, 4, 3, 9, 6},   {1, 4, 4, 7, 9},  {1, 4, 6, 9, 7},
        {1, 4, 1, 2, 10},   {1, 4, 1, 10, 4},  {1, 4, 2, 5, 10}, {1, 4, 4, 10, 5},
        {2, 4, 4, 5, 8, 7}, {2, 4, 0, 1, 4, 3}};

    print2D(conns.cbegin(), conns.cend(), "\n\n\tbefore\n");

    std::stable_sort(conns.begin(), conns.end(), [=](const auto& a, const auto& b) {
        std::cout << index << "\ta.size=" << a.size() << " b.size=" << b.size() << "\n";
        return a[index] < b[index];
    });

    print2D(conns.cbegin(), conns.cend(), "\n\n\tafter\n");
}
Run Code Online (Sandbox Code Playgroud)

输出:

   before
     0    2     0   1
     0    2     1   2
     0    1     2   5
     0    1     5   8
     0    3     8   7
     0    3     7   6
     0    0     6   3
     0    0     3   0
     1    4     3   4    9
     1    4     3   9    6
     1    4     4   7    9
     1    4     6   9    7
     1    4     1   2   10
     1    4     1  10    4
     1    4     2   5   10
     1    4     4  10    5
     2    4     4   5    8      7
     2    4     0   1    4      3

1  a.size=4 b.size=0
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)