[Spirit] dereferencing type-punned pointer will break strict-aliasing rules
Hi, compiling the attached example with gcc-4.4.3 (Ubuntu 10.4, 64bit) with -Wall -O3 leads to a long warning message (second attachment), culminating in boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules I wonder how I could avoid this without -fno-strict-aliasing or -Wno-strict-aliasing? Thanks and regards, Roland
On Wed, Aug 11, 2010 at 1:37 AM, Roland Bock
Hi,
compiling the attached example with gcc-4.4.3 (Ubuntu 10.4, 64bit) with
-Wall -O3
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
I wonder how I could avoid this without -fno-strict-aliasing or -Wno-strict-aliasing?
The code in question is a reinterpret_cast which casts a char pointer to a user-specified functor type pointer. The interpret cast seems correct, and the warning seems correct too if not very useful in this case. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
At Wed, 11 Aug 2010 11:59:21 -0700, Emil Dotchevski wrote:
On Wed, Aug 11, 2010 at 1:37 AM, Roland Bock
wrote: Hi,
compiling the attached example with gcc-4.4.3 (Ubuntu 10.4, 64bit) with
-Wall -O3
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
I wonder how I could avoid this without -fno-strict-aliasing or -Wno-strict-aliasing?
The code in question is a reinterpret_cast which casts a char pointer to a user-specified functor type pointer. The interpret cast seems correct, and the warning seems correct too if not very useful in this case.
Really, the reinterpret_cast is portably correct? Pretty much the only thing you can do portably with reinterpret_cast is round-trip cast a pointer type to another pointer type and then cast it back before you use it again. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
On Wed, Aug 11, 2010 at 3:14 PM, David Abrahams
At Wed, 11 Aug 2010 11:59:21 -0700, Emil Dotchevski wrote:
On Wed, Aug 11, 2010 at 1:37 AM, Roland Bock
wrote: Hi,
compiling the attached example with gcc-4.4.3 (Ubuntu 10.4, 64bit) with
-Wall -O3
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
I wonder how I could avoid this without -fno-strict-aliasing or -Wno-strict-aliasing?
The code in question is a reinterpret_cast which casts a char pointer to a user-specified functor type pointer. The interpret cast seems correct, and the warning seems correct too if not very useful in this case.
Really, the reinterpret_cast is portably correct? Pretty much the only thing you can do portably with reinterpret_cast is round-trip cast a pointer type to another pointer type and then cast it back before you use it again.
That's what this code does. Honestly, I'm probably giving the compiler too much credit, since I assumed that it sees that the object it's taking the address of is a single char, not an array of chars. In reality it's probably not that clever and the warning is plain wrong because in general strict aliasing rules don't apply to char pointers, do they? Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
At Wed, 11 Aug 2010 16:07:31 -0700, Emil Dotchevski wrote:
Really, the reinterpret_cast is portably correct? Pretty much the only thing you can do portably with reinterpret_cast is round-trip cast a pointer type to another pointer type and then cast it back before you use it again.
That's what this code does.
Honestly, I'm probably giving the compiler too much credit, since I assumed that it sees that the object it's taking the address of is a single char, not an array of chars. In reality it's probably not that clever and the warning is plain wrong because in general strict aliasing rules don't apply to char pointers, do they?
I dunno, sorry. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
On 8/12/10 2:59 AM, Emil Dotchevski wrote:
On Wed, Aug 11, 2010 at 1:37 AM, Roland Bock
wrote: Hi,
compiling the attached example with gcc-4.4.3 (Ubuntu 10.4, 64bit) with
-Wall -O3
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
I wonder how I could avoid this without -fno-strict-aliasing or -Wno-strict-aliasing?
The code in question is a reinterpret_cast which casts a char pointer to a user-specified functor type pointer. The interpret cast seems correct, and the warning seems correct too if not very useful in this case.
Shouldn't this thread have a [function] subject as this seems to be about boost.function issue? Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
On 08/12/2010 06:05 AM, Joel de Guzman wrote:
On 8/12/10 2:59 AM, Emil Dotchevski wrote:
On Wed, Aug 11, 2010 at 1:37 AM, Roland Bock
wrote: Hi,
compiling the attached example with gcc-4.4.3 (Ubuntu 10.4, 64bit) with
-Wall -O3
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
I wonder how I could avoid this without -fno-strict-aliasing or -Wno-strict-aliasing?
The code in question is a reinterpret_cast which casts a char pointer to a user-specified functor type pointer. The interpret cast seems correct, and the warning seems correct too if not very useful in this case.
Shouldn't this thread have a [function] subject as this seems to be about boost.function issue?
Maybe. Not sure since the warning occurred in a Spirit context. My code does not use anything of boost.function directly. Based on the hints so far, I'll investigate some more. Regards, Roland
Roland, On Wednesday 11 August 2010 01:37:17 Roland Bock wrote:
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
This warning can be prevented by splitting the line into two lines as follows:
Replace:
reinterpret_cast
On 08/12/2010 08:26 AM, KSpam wrote:
Roland,
On Wednesday 11 August 2010 01:37:17 Roland Bock wrote:
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
This warning can be prevented by splitting the line into two lines as follows:
Replace: reinterpret_cast
(&in_buffer.data)->~Functor(); with: functor_type* p = reinterpret_cast
(&in_buffer.data); p->~Functor(); I patched my local version of Boost in order to prevent these warnings.
Hope This Helps, Justin
Justin, thanks for the hint! I'll give it a try and will probably file a ticket if it works out :-) Regards, Roland
On Thu, Aug 12, 2010 at 12:48 AM, Roland Bock
On 08/12/2010 08:26 AM, KSpam wrote:
Roland,
On Wednesday 11 August 2010 01:37:17 Roland Bock wrote:
leads to a long warning message (second attachment), culminating in
boost/1.44/include/boost/function/function_base.hpp:321: warning: dereferencing type-punned pointer will break strict-aliasing rules boost/1.44/include/boost/function/function_base.hpp:325: warning: dereferencing type-punned pointer will break strict-aliasing rules
This warning can be prevented by splitting the line into two lines as follows:
Replace: reinterpret_cast
(&in_buffer.data)->~Functor(); with: functor_type* p = reinterpret_cast
(&in_buffer.data); p->~Functor(); I patched my local version of Boost in order to prevent these warnings.
Hope This Helps, Justin
Justin,
thanks for the hint! I'll give it a try and will probably file a ticket if it works out :-)
Also, you should file a ticket with GCC, two tickets actually: 1) a trivial change like this should not have effect on the warning, and 2) strict aliasing rules should not apply to char pointers (as far as I can tell, in_buffer.data is of char type specifically to deal with this issue.) Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 12 August 2010, Emil Dotchevski wrote:
2) strict aliasing rules should not apply to char pointers (as far as I can tell, in_buffer.data is of char type specifically to deal with this issue.)
It's safe to cast to a char pointer and use the char pointer, but in this case you are casting away from a char pointer. Bouncing a non-char pointer through a char pointer doesn't give any relief from the usual strict aliasing rules. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkxkP1YACgkQ5vihyNWuA4VslQCfccDLWRNcgpH0OhfT9qxeXJXA dbQAnjEb+bwpRdUH/2myqMfWMYmBJbCP =D2e7 -----END PGP SIGNATURE-----
On Thu, Aug 12, 2010 at 11:37 AM, Frank Mori Hess
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Thursday 12 August 2010, Emil Dotchevski wrote:
2) strict aliasing rules should not apply to char pointers (as far as I can tell, in_buffer.data is of char type specifically to deal with this issue.)
It's safe to cast to a char pointer and use the char pointer, but in this case you are casting away from a char pointer. Bouncing a non-char pointer through a char pointer doesn't give any relief from the usual strict aliasing rules.
The object is not accessed through a char pointer, the char pointer is only used to define a memory location. The char object itself is used only to take its address. Even if the warning was valid, it just means that *if* you access the same object through different types (which the code never does), you'd be in trouble; however section 6.5, paragraph 7 of the C standard says that any object may be aliased by an object of type char, so the warning seems invalid. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (6)
-
David Abrahams
-
Emil Dotchevski
-
Frank Mori Hess
-
Joel de Guzman
-
KSpam
-
Roland Bock