public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: is portable aliasing possible in C++?
@ 2014-09-09 23:13 haynberg
  2014-09-10  8:17 ` Andrew Haley
  0 siblings, 1 reply; 35+ messages in thread
From: haynberg @ 2014-09-09 23:13 UTC (permalink / raw)
  To: gcc-help


Using reinterpret_cast to convert between unrelated pointer types is a language feature that, by definition, makes your code not portable (assuming you dereference).

While it's undefined by the standard, that doesn't mean it's undefined for a given platform (OS/compiler/compiler options).  For example, on an architecture which allows unaligned reads and the compiler does not perform strict-aliasing optimizations, it's likely OK (e.g. MS or Clang on x86).  If the compiler does perform those optimizations and provides a way to programmatically disable them (like GCC's may_alias) and you make use of that feature correctly, it's also likely OK.  But if the compiler does perform those optimizations and it doesn't provide something like may_alias, then it is undefined, and you have no choice but to disable the optimization for the whole program or resort to memcpy.

So it would seem the answer is no, portable aliasing is not possible in C++.  Though I wonder if it should be (with a new language feature).

Jay Haynberg

^ permalink raw reply	[flat|nested] 35+ messages in thread
* RE: is portable aliasing possible in C++?
@ 2014-09-10 23:03 haynberg
  2014-09-11  8:11 ` Andrew Haley
  0 siblings, 1 reply; 35+ messages in thread
From: haynberg @ 2014-09-10 23:03 UTC (permalink / raw)
  To: aph, gcc-help

> > So it would seem the answer is no, portable aliasing is not possible
> > in C++.  Though I wonder if it should be (with a new language
> > feature).
>
> In practice, a union is exactly that, and I've not heard of a compiler
> which doesn't do the right thing.  Given that it's explicitly legal in
> C11, I can't see any reason which C++ shouldn't simply adopt the same
> language.

I agree.  But in the union case, you're copying bytes.  If you don't perform 

a copy, for example:

struct msg {
  // ...
};
char *get_bytes();
msg *p = reinterpret_cast<msg*>(get_bytes());
if (p->i)
  // ...

Then there can't be a portable way to do this, because there's hardware that 

doesn't permit unaligned reads (e.g. where you'd get a SIGBUS).  So I guess 

I retract that "I wonder if" part :-)

Jay Haynberg

^ permalink raw reply	[flat|nested] 35+ messages in thread
[parent not found: <A76FB9DDEDFA994BAF6B77704A4AF465BC2464@xchmbbal502.ds.susq.com>]

end of thread, other threads:[~2014-11-03  9:34 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-09 23:13 is portable aliasing possible in C++? haynberg
2014-09-10  8:17 ` Andrew Haley
  -- strict thread matches above, loose matches on Subject: below --
2014-09-10 23:03 haynberg
2014-09-11  8:11 ` Andrew Haley
2014-09-11 23:25   ` haynberg
2014-09-12  8:32     ` Andrew Haley
2014-09-12 22:58       ` haynberg
2014-09-13  7:23         ` Andrew Haley
2014-09-13 11:45           ` Oleg Endo
2014-09-15  2:37           ` Hei Chan
2014-09-15  8:35             ` Andrew Haley
2014-09-15 11:07               ` Hei Chan
2014-09-15 11:21                 ` Andrew Haley
2014-09-15 11:29                   ` Hei Chan
2014-09-15 11:32                     ` Andrew Haley
2014-09-15 11:57                       ` Hei Chan
2014-09-15 13:21                         ` Andrew Haley
2014-09-15 13:31                           ` Hei Chan
2014-09-15 14:11                             ` Andrew Haley
2014-09-15 11:27                 ` Jonathan Wakely
2014-09-15 12:09                   ` Paul Smith
2014-11-02 23:55       ` Hei Chan
2014-11-03  9:34         ` Andrew Haley
     [not found] <A76FB9DDEDFA994BAF6B77704A4AF465BC2464@xchmbbal502.ds.susq.com>
2014-09-04 16:11 ` Andy Webber
2014-09-04 16:51   ` Andrew Haley
2014-09-04 17:18     ` Andy Webber
2014-09-04 17:23       ` Andrew Haley
2014-09-04 17:44         ` Andy Webber
2014-09-04 17:47           ` Andy Webber
2014-09-04 17:48           ` Andrew Haley
2014-09-04 23:11     ` Jonathan Wakely
2014-09-05  7:16       ` Andrew Haley
2014-09-05 14:19       ` Jason Merrill
2014-09-08  9:33         ` Richard Biener
2014-09-10 14:31           ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).