From: Seungbeom Kim on 31 Mar 2010 23:08 I get this warning while compiling the program below with g++-4.4.3: $ g++ -O2 -Wall test.cc test.cc: In function �int main()�: test:8: warning: dereferencing pointer �<anonymous>� does break strict-aliasing rules /usr/include/c++/4.4/bits/stl_list.h:214: note: initialized from here I don�t see anything suspicious in the following program, but there should be a reason for the warning. Can anyone explain this? /* 1 */ #include <list> /* 2 */ /* 3 */ struct X /* 4 */ { /* 5 */ struct P /* 6 */ { /* 7 */ const void* p; /* 8 */ bool operator==(const P& o) const { return p == o.p; } /* 9 */ }; /* 10 */ /* 11 */ typedef std::list<P> PL; /* 12 */ /* 13 */ struct I /* 14 */ { /* 15 */ PL::const_iterator itr; /* 16 */ bool operator==(I o) const { return *itr == *o.itr; } /* 17 */ bool operator!=(I o) const { return !(*this == o); } /* 18 */ I& operator++() { ++itr; return *this; } /* 19 */ }; /* 20 */ /* 21 */ PL list; /* 22 */ /* 23 */ I begin() const { I i = {list.begin()}; return i; } /* 24 */ I end() const { I i = {list.end()}; return i; } /* 25 */ }; /* 26 */ /* 27 */ int main() /* 28 */ { /* 29 */ X x; /* 30 */ for (X::I it = x.begin(); it != x.end(); ++it) { } /* 31 */ } Line 214 of /usr/include/c++/4.4/bits/stl_list.h looks like this: /* 182 */ /** /* 183 */ * @brief A list::const_iterator. /* 184 */ * /* 185 */ * All the functions are op overloads. /* 186 */ */ /* 187 */ template<typename _Tp> /* 188 */ struct _List_const_iterator /* 189 */ { /* ... */ /* 210 */ // Must downcast from List_node_base to _List_node to get to /* 211 */ // _M_data. /* 212 */ reference /* 213 */ operator*() const /* 214 */ { return static_cast<_Node*>(_M_node)->_M_data; } /* ... */ /* 260 */ }; $ g++ --version g++ (Debian 4.4.2-9) 4.4.3 20100108 (prerelease) Copyright (C) 2009 Free Software Foundation, Inc. .... -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Paul Bibbings on 1 Apr 2010 06:56 Seungbeom Kim <musiphil(a)bawi.org> writes: > I get this warning while compiling the program below with g++-4.4.3: > > $ g++ -O2 -Wall test.cc > test.cc: In function ��int main()��: > test:8: warning: dereferencing pointer ��<anonymous>�� does break > strict-aliasing rules > /usr/include/c++/4.4/bits/stl_list.h:214: note: initialized from here > > I don��t see anything suspicious in the following program, but there > should be a reason for the warning. Can anyone explain this? I get the same with gcc-4.4.3 built for Cygwin on Windows. However, what seems strange is that I apparently can avoid this warning simply by building in two separate steps: 21:27:45 Paul Bibbings(a)JIJOU /cygdrive/d/CPPProjects/CLCPPM $gcc -Wall -c test.cc 21:27:58 Paul Bibbings(a)JIJOU /cygdrive/d/CPPProjects/CLCPPM $g++ -O2 test.o <snip>code - see OP</snip> > > $ g++ --version > g++ (Debian 4.4.2-9) 4.4.3 20100108 (prerelease) > Copyright (C) 2009 Free Software Foundation, Inc. > ... 21:28:16 Paul Bibbings(a)JIJOU /cygdrive/d/CPPProjects/CLCPPM $g++ --version g++ (GCC) 4.4.3 Copyright (C) 2010 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. Regards Paul Bibbings -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 1 Apr 2010 20:41 On 2010-04-01 14:56, Paul Bibbings wrote: > Seungbeom Kim<musiphil(a)bawi.org> writes: > >> I get this warning while compiling the program below with g++-4.4.3: >> >> $ g++ -O2 -Wall test.cc >> test.cc: In function �int main()�: >> test:8: warning: dereferencing pointer �<anonymous>� does break >> strict-aliasing rules >> /usr/include/c++/4.4/bits/stl_list.h:214: note: initialized from here >> >> I don�t see anything suspicious in the following program, but there >> should be a reason for the warning. Can anyone explain this? > > I get the same with gcc-4.4.3 built for Cygwin on Windows. However, > what seems strange is that I apparently can avoid this warning simply by > building in two separate steps: > > 21:27:45 Paul Bibbings(a)JIJOU > /cygdrive/d/CPPProjects/CLCPPM $gcc -Wall -c test.cc > > 21:27:58 Paul Bibbings(a)JIJOU > /cygdrive/d/CPPProjects/CLCPPM $g++ -O2 test.o That�s because you omitted -O2 when you were compiling; specifically, -O2 turns on -fstrict-aliasing. As far as I know, -fstrict-aliasing affects the compiling stage, not the linking stage, so what you�re doing is basically turning off -fstrict-aliasing, which is why you�re not getting the warning. Turning off that optimization could be an answer, but I�d like to know whether the warning is just a false positive or it really means I risk some undefined behaviour and I should do something (turn the optimization off or change the code somehow). -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mickey on 1 Apr 2010 20:44 On Apr 2, 2:56 am, Paul Bibbings <paul.bibbi...(a)gmail.com> wrote: > Seungbeom Kim <musip...(a)bawi.org> writes: > > I get this warning while compiling the program below with g++-4.4.3: > > > $ g++ -O2 -Wall test.cc > > test.cc: In function �int main()�: > > test:8: warning: dereferencing pointer �<anonymous>� does break > > strict-aliasing rules > > /usr/include/c++/4.4/bits/stl_list.h:214: note: initialized from here > > > I don�t see anything suspicious in the following program, but there > > should be a reason for the warning. Can anyone explain this? > > I get the same with gcc-4.4.3 built for Cygwin on Windows. However, > what seems strange is that I apparently can avoid this warning simply by > building in two separate steps: > > 21:27:45 Paul Bibbings(a)JIJOU > /cygdrive/d/CPPProjects/CLCPPM $gcc -Wall -c test.cc > > 21:27:58 Paul Bibbings(a)JIJOU > /cygdrive/d/CPPProjects/CLCPPM $g++ -O2 test.o > > <snip>code - see OP</snip> > > > > > $ g++ --version > > g++ (Debian 4.4.2-9) 4.4.3 20100108 (prerelease) > > Copyright (C) 2009 Free Software Foundation, Inc. > > ... > > 21:28:16 Paul Bibbings(a)JIJOU > /cygdrive/d/CPPProjects/CLCPPM $g++ --version > g++ (GCC) 4.4.3 > Copyright (C) 2010 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. { edits: quoted sig & banner removed. please keep readers in mind when you quote. -mod } With gcc 4.4.1 I am not able to reproduce the issue: jyoti(a)vbox:~$ g++ --version g++ (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. jyoti(a)vbox:~$ g++ -Wall --pedantic test.cpp -o test jyoti(a)vbox:~$ ./test jyoti(a)vbox:~$ Understanding strict aliasing might help: http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html Regards, Jyoti -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 2 Apr 2010 07:09
On 2010-04-02 04:44, Mickey wrote: > > With gcc 4.4.1 I am not able to reproduce the issue: > > jyoti(a)vbox:~$ g++ --version > g++ (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. > > jyoti(a)vbox:~$ g++ -Wall --pedantic test.cpp -o test > jyoti(a)vbox:~$ ./test > jyoti(a)vbox:~$ Did you try -O2 or -fstrict-aliasing? It is crucial that you turn it on. > Understanding strict aliasing might help: > http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html I know what strict aliasing is, and had read that article too, but it doesn't help me understanding this particular case. -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |