G 9.1更新后的STD :: stable_sort的问题

Problem with std::stable_sort after g++ 9.1 update

本文关键字:stable sort 问题 STD 更新      更新时间:2023-10-16

从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
}

其中 conns 是存储元素连接性的std::vector<std::vector<int>>。分类是根据列 index 进行的,其值是在类标题中分配的,并且所有 std::vector<int> in conns 具有该条目。

值得一提的另一个事实是,在上> debug builds build(Compiler flag" -g"使用," -o3"不是(所有测试成功。p>另外,通过打印呼叫之前和之后的 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

之后
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

on debug 构建,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(), "nntbeforen");
    std::stable_sort(conns.begin(), conns.end(), 
        [=](const auto& a, const auto& b){
            return a[index] < b[index];
        }
    );
    print2D(conns.cbegin(), conns.cend(), "nntaftern");
    return 0;
}

如果上述用

编译
    g++ -o main main.cpp -m64 -std=c++17 -O3

它输出segmentation fault (core dumped)。但是,如果未使用标志" -O3",则获得了预期的结果。

使用G 9.1.1编译时,第一个" b"将传递到Lambda中,该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(), "nntbeforen");
    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(), "nntaftern");
}

输出:

   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)