public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Evan Jones <evanj@MIT.EDU>
To: gcc-help@gcc.gnu.org
Subject: strict aliasing: how to swap pointers
Date: Tue, 29 Apr 2008 23:58:00 -0000	[thread overview]
Message-ID: <481785A7.3060506@mit.edu> (raw)

I have a function to swap any two pointers (assuming that sizeof(void*) 
== sizeof(T*)). I would like to use it without violating strict aliasing 
rules. I have a workaround, but I would like to verify that this is the 
right way to do this.

The test case:

void* exchange(void** ptr, void* next);

int main() {
     int v1 = 42;
     int v2 = 123;

     int* a = &v1;

     int before = *a;
     exchange(reinterpret_cast<void**>(&a), &v2);
     int after = *a;

     printf("before: %d after: %d\n", before, after);

     return 0;
}


Output from Redhat's version of gcc 4.1.2 20070925:

test.cc: In function ‘int main()’:
test.cc:16: warning: dereferencing type-punned pointer will break 
strict-aliasing rules


The output from the program is correct. My workaround avoids the 
warning, but I think it will still permit GCC to generate undesired 
optimizations, since it says nothing about the *values* pointed to:


     union {
         int** int_ptr;
         void** void_ptr;
     } temp = { &a };
     exchange(temp.void_ptr, &v2);


I believe this may be incorrect, since *int_ptr and *void_ptr have 
distinct types, and hence GCC could decide that they cannot possibly be 
aliases? What is the "right" way to fix this? Thanks for your help,

Evan Jones

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

             reply	other threads:[~2008-04-29 20:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-29 23:58 Evan Jones [this message]
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
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=481785A7.3060506@mit.edu \
    --to=evanj@mit.edu \
    --cc=gcc-help@gcc.gnu.org \
    /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).