[英]g++ optimization bug on osx using -O3
The following code produces the wrong exit code when compiled with -O3. 使用-O3编译时,以下代码将产生错误的退出代码。 I think the inner loop is being optimized away incorrectly.
我认为内循环被错误地优化了。 With -O2 or -fno-inline it works.
使用-O2或-fno-inline可以正常工作。 Producing a simpler example is difficult because any small changes and the bug disappears.
产生一个简单的示例很困难,因为任何小的更改都会使错误消失。
Compiled with: 编译:
/usr/bin/g++ -O3 -o bugexample bugexample.cpp
Code: 码:
#include <vector>
int test(std::vector<char>& a, int& b)
{
std::vector<int> z;
z.push_back(10);
z.push_back(10);
int d = (int)a.size();
int x = 1;
for (int j = 0; j < 2; j++)
{
int c = j - 1;
for (int i = 0; i < d; i++)
{
if (j == 0)
{
}
else if (i == 0)
{
}
else
{
if (a[j] == a[i - 1])
{
b = c + 1;
x = 2;
}
z[i] = 1;
}
}
}
return x;
}
int main(int argc, char* argv[])
{
std::vector<char> a;
a.push_back('a');
a.push_back('a');
int b = 1;
return test(a,b);
}
Compiler version: 编译器版本:
/usr/bin/g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~123/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
Interested in any insite, or proof that its my fault. 对任何现场感兴趣,或证明它是我的错。
Edit: The exit code produced is 1, whereas it should b 2. 编辑:产生的退出代码为1,而应为b 2。
Mmm is this some kind of obfuscated code contest? 嗯,这是某种混淆代码竞赛吗?
As far as I can tell you're trying to do some kind of palindrome test on the input vector. 据我所知,您正在尝试对输入向量进行某种回文检验。 Only,
只要,
j
has a hardcoded upperbound of 2
(which should probably have been a.size() as well?) j
的硬编码上限为2
(应该也应该是a.size()?) z
vector z
向量 b
; b
; I replaced that bool
return type with the value of b
(with b==-1 indicating no match found) b
的值替换了bool
返回类型(b ==-1表示找不到匹配项) When simplifying the code for these things, I get this code, and (like your own code) it behaves identically for all optimization levels on g++ 4.6.1: 在简化这些代码时,我得到了这段代码,并且(就像您自己的代码一样),对于g ++ 4.6.1上的所有优化级别,其行为都相同:
#include <vector>
int test(const std::vector<char>& a)
{
/* int j = 1; // was: for (int j = 1; j < 2; j++) */
for (int i = a.size()-1; i > 1; i--)
if (a[1] == a[i - 1])
return 1;
return -1;
}
int main(int argc, char* argv[])
{
std::vector<char> a(2, 'a');
int b = test(a);
return b==-1? 1 : 2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.