尝试理解 GCC 错误

发布于 2024-08-18 08:52:24 字数 1429 浏览 6 评论 0原文

我有以下代码:

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

template<typename Iterator>
void foo(Iterator begin, Iterator end)
{
   typedef typename std::iterator_traits<Iterator>::value_type type;
   type smallest = (*std::min_element(begin,end));
   std::cout << smallest << std::endl;
}

int main()
{
   std::list<int> l;
   l.push_back(1);   
   l.push_back(2);
   foo(l.begin(),l.end());
   return 0;
}

当我按如下方式编译它时:

g++ -pedantic -ansi -Wall -Werror -O2 -o  test test.cpp

我收到以下错误:

cc1plus: warnings being treated as errors
In function ‘int main()’:
cc1plus: error: dereferencing pointer ‘pretmp.163’ does break strict-aliasing rules
cc1plus: note: initialized from here

O3 会出现此错误,但 O1 不会出现此错误。我使用 comeau 在线编译器、MS VC9.0 和 icc v11 编译了代码,在所有情况下代码编译都没有问题。

该代码与 std::vectorstd::dequestd::setchar* 配合使用效果良好, int* 迭代器,似乎是 std::list 实现的非常特定的东西。

我希望有人能够深入了解这个特定错误(警告)的含义以及如何解决它。

注意:GCC 版本是:

gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I have the following bit of code:

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

template<typename Iterator>
void foo(Iterator begin, Iterator end)
{
   typedef typename std::iterator_traits<Iterator>::value_type type;
   type smallest = (*std::min_element(begin,end));
   std::cout << smallest << std::endl;
}

int main()
{
   std::list<int> l;
   l.push_back(1);   
   l.push_back(2);
   foo(l.begin(),l.end());
   return 0;
}

when I compile it as follows:

g++ -pedantic -ansi -Wall -Werror -O2 -o  test test.cpp

I get the following error:

cc1plus: warnings being treated as errors
In function ‘int main()’:
cc1plus: error: dereferencing pointer ‘pretmp.163’ does break strict-aliasing rules
cc1plus: note: initialized from here

This error is seen with O3, but not with O1. I've compiled the code using the comeau online compiler, MS VC9.0 and icc v11 and in all cases the code compiles without an issue.

The code works fine with std::vector, std::deque, std::set, char*, int* iterators, seems to be something very specific to the implementation of std::list.

I was hoping someone could provide some insight into what this particular error(warning) means and how to go about resolving it.

Note: The GCC version is:

gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谎言 2024-08-25 08:52:25

这是一个 bugzilla 案例,似乎代表了这个问题或非常类似的问题:

它在 4.4.0 中被标记为已修复,因此您要么遇到了另一个极端情况,要么???。但这个错误可能会给你一个解决方案提供帮助。

Here's a bugzilla case that seems to represent this problem or something very similar:

It's marked as fixed in 4.4.0, so you've either run into another corner case, or ???. But this bug might give you a leg up on a resolution for you.

海的爱人是光 2024-08-25 08:52:24

我已经在 gcc-4.4 上复制了您的错误。对于 gcc-4.5 和 gcc-4.6,这不是一个错误,来自 Debian“不稳定”。

我遇到了一个特定于 gcc-4.4 的相关错误,类似于此提交的错误: 错误 42488 – [仅限 4.4] 虚假严格别名警告。正如 @michael-burr 指出的那样,gcc-4.4 存在大量“严格别名规则”错误:GCC 错误列表:严格别名规则 4.4

我意识到这有点令人不满意,但我已经开始接受这是 GCC 4.4 中的一个错误,并且能够转移到不存在此问题的新版本。

I've replicated your error on gcc-4.4. This is a non-error with gcc-4.5 and gcc-4.6, from Debian "unstable."

I've experienced a somewhat related bug specific to gcc-4.4, similar to this filed bug: Bug 42488 – [4.4 only] spurious strict-aliasing warning. As @michael-burr pointed out, there are a large number of "strict-aliasing rules" bugs against gcc-4.4: GCC Bug List: strict aliasing rules 4.4.

I realize it's a bit unsatisfying, but I've come to accept this as a bug in GCC 4.4, and been able to move onto newer versions that don't have this problem.

傻比既视感 2024-08-25 08:52:24

没有关于 gnu 的 clu ...但这似乎很有用: http:// /code.google.com/p/v8/issues/detail?id=413

No clu 'bout gnu ... but this seems useful : http://code.google.com/p/v8/issues/detail?id=413

阳光①夏 2024-08-25 08:52:24

只是冒险猜测:如果您将 foo 的 args 声明为 const,则可能会解决此问题。如果我理解正确的话,当循环中的多个指针可能指向相同的数据时,就会出现“别名”问题 - 这会导致潜在的操作顺序错误以及一些优化(这就是警告所指的内容) )。

Just hazarding a guess: if you declared the args of foo to be const, it may fix this issue. The "aliasing" issue, if I understand it correctly, comes up when more than one pointer in a loop could be pointing to the same data - this leads to potential order of operation bugs with some optimizations (which is what the warning is referring to).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文