从 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>>存储元素连接。排序是根据索引列进行的,它的值在类头中赋值,所有std::vector<int>在 conns 中有该条目。
另一个值得一提的事实是,在调试 构建(使用编译器标志“-g”,而不是“-O3”)所有测试成功。。 p>
此外,在release 版本中(使用标志“-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
之后
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
在调试 构建中,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;
}
如果上面的是用
编译的 g++ -o main main.cpp -m64 -std=c++17 -O3
它输出 segmentation fault (core dumped) .但是,如果不使用标志“-O3”,则会获得预期的结果。
最佳答案
第一个“b”,当使用 g++ 9.1.1 编译时,传递到 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");
}
输出:
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)
关于c++ - g++ 9.1 更新后 std::stable_sort 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56779605/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。