G 9.1更新后的STD :: stable_sort的问题
Problem with std::stable_sort after g++ 9.1 update
从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)
- 为什么 std::unique 不调用 std::sort?
- 对字符串进行排序时,在c++中处理sort()
- std::sort()函数无法对向量的一部分进行排序
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- C++中"std::sort"比较器的不同类型
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- 按字母顺序对字符串中的字母进行排序,而无需使用内置的 sort()
- c++无值sort()的问题是什么?
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 使用 std::sort 对向量进行稳定排序
- 如何使用 QSortFilterProxyModel::sort 对 Qlist 中的数据进行排序
- std::sort 如何处理重复的数字?
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- C++ <algorithm> 使用对象作为比较定义的 sort()
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- unqualified sort() -- 为什么它在 std::vector 上使用而不是在 std::array 上
- 使用 std::sort 对二维 c 数组进行排序
- std::stable_sort vs std::sort
- std::sort 导致运算符分段错误<
- 使用 std::sort 对 C 样式的 2D 数组进行部分排序