public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Evan Jones <evanj@MIT.EDU>
To: Andrew Haley <aph@redhat.com>, Sergei Organov <osv@javad.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: strict aliasing: how to swap pointers
Date: Wed, 30 Apr 2008 14:43:00 -0000	[thread overview]
Message-ID: <4818746C.5040106@mit.edu> (raw)
In-Reply-To: <4818340F.1080100@redhat.com>

On Apr 30, 2008, at 4:55 , Andrew Haley wrote:
> We have no way to know, since you didn't provide us with the source of
> exchange(), and that's where the aliasing violation, if any, would
> occur.

Oops! Sorry, I meant to. The simplest implementation is a swap using a 
temporary:

void* exchange(void** ptr, void* next) {
  void* old = *ptr;
  *ptr = next;
  return old;
}

> However, the answer is almost certainly no.  I don't know why you're
> trying to do something so simple in such a difficult way.  If you really
> want to do this in standard portable C, the easiest way is a macro:

I have implementations of exchange() that either use a temporary, a 
compare-and-swap, a load-linked/store-conditional, or a mutex. There is 
some project-specific configuration to link against the appropriate 
implementation.

On Apr 30, 2008, at 12:18, Sergei Organov wrote:
> Now, here is a solution that does not break strict aliasing rules:
>
> $ cat alias.cc
> #include <cstdio>
> #include <cstring>
>
> using namespace std;
>
> void swap(void* p1, void* p2)
> {
>   void* t;
>   memcpy(&t, p1, sizeof(t));
>   memcpy(p1, p2, sizeof(t));
>   memcpy(p2, &t, sizeof(t));
> }

This seems reasonable. In my version, I would simply return t, rather 
than using the last memcpy.

What is confusing to me about the strict aliasing warning is that I 
thought GCC must conservatively assume that a void* pointer can point to 
anything, since T* is convertible to void*. Hence, it seems to me that 
casting a T** to void** should not result in a type-punning warning.

Additionally I'm confused because the warning is quite "fragile." This 
causes the warning:

void** voidptrptr = reinterpret_cast<void**>(&intptr);

This does not:

int** intptrptr = &intptr;
void** voidptrptr = reinterpret_cast<void**>(intptrptr);

To me, these appear to be equivalent.

Thanks for the help, I appreciate it.

Evan Jones

--
Evan Jones
http://evanjones.ca/


  parent reply	other threads:[~2008-04-30 13:31 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-29 23:58 Evan Jones
2008-04-30  1:00 ` Rupert Wood
2008-04-30  1:17   ` OT Rant on why C++ Sucks (Was Re: strict aliasing: how to swap pointers) Robert William Fuller
2008-04-30  3:03     ` Gareth Buxton
2008-04-30  7:59     ` me22
2008-04-30 10:21       ` Rupert Wood
2008-04-30  4:46   ` strict aliasing: how to swap pointers Evan Jones
2008-04-30 10:13     ` Andrew Haley
2008-04-30 11:45       ` Andrew Haley
2008-04-30 13:31         ` Jędrzej Dudkiewicz
2008-04-30 14:29           ` Andrew Haley
2008-04-30 14:43       ` Evan Jones [this message]
2008-04-30 14:49         ` John Love-Jensen
2008-04-30 15:15           ` Evan Jones
2008-04-30 15:33             ` Andrew Haley
2008-04-30 18:01               ` Evan Jones
2008-04-30 20:38                 ` Andrew Haley
2008-04-30 21:49                 ` Sergei Organov
2008-04-30 15:22         ` Andrew Haley
2008-05-03  0:45           ` Matthew Woehlke
2008-05-03  9:10             ` Andrew Haley
2008-04-30  9:03 ` Sergei Organov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4818746C.5040106@mit.edu \
    --to=evanj@mit.edu \
    --cc=aph@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=osv@javad.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).