public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gabriel at teuton dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/36566] Cannot bind packed field
Date: Sat, 14 Sep 2013 23:08:00 -0000	[thread overview]
Message-ID: <bug-36566-4-IwvXBtO7D0@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-36566-4@http.gcc.gnu.org/bugzilla/>

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36566

--- Comment #6 from Gabriel M. Beddingfield <gabriel at teuton dot org> ---
All assignments of obj.s to type short& and short* are incorrect, and ideally
they would all result in compiler errors.

The C++ spec (C++03, Sects. 3.9, 3.9.1, 3.9.2) are very clear that T and
"pointer to T" have implementation-specific alignment requirements.  If you
have a "pointer to T" then you may assume that it meets the alignment
requirements.  I'm sure the C spec has similar language.

In the OP's case, the following code could violate the alignment requirements:

    short& pit(obj.s); /* invalid */

For example &obj.s could have an address 0x602011 (which has 1-byte
alignement).  When you assign that pointer to pit... pit is now a reference
(i.e. pointer) that violates the alignment requirements of short (which usually
requires 2-byte alignment).

Why is this a problem?  On x86/x86_64 it's *usually* no big deal because the
CPU will gracefully handle unaligned memory access (with a performance
penalty).  On less forgiving hardware platforms, trying to use `pit' will
result in illegal instruction exceptions.

You can pass the reference if you change the function prototype to something
like:

    typedef short un_short __attribute__((alignment(1)));
    void VerticallyChallenged(un_short&) {}

...and then call it with a cast like this:

    VerticallyChallenged((un_short&)oj.s);

[amazing, the kind of stuff you learn over the course of 4 years :-)]


  parent reply	other threads:[~2013-09-14 23:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-36566-4@http.gcc.gnu.org/bugzilla/>
2013-09-13  4:14 ` structurechart at yahoo dot com
2013-09-14 23:08 ` gabriel at teuton dot org [this message]
2015-03-18  0:53 ` xiaoj at google dot com
2015-03-18  1:56 ` redi at gcc dot gnu.org
2021-12-31 15:11 ` steve+gcc at tecwec dot eu
2008-06-18 18:28 [Bug c++/36566] New: " nevin at eviloverlord dot com
2008-06-18 18:31 ` [Bug c++/36566] " pinskia at gcc dot gnu dot org
2008-06-18 18:49 ` nevin at eviloverlord dot com
2008-06-18 19:06 ` nevin at eviloverlord dot com
2009-10-11  4:38 ` gabriel at teuton dot org

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=bug-36566-4-IwvXBtO7D0@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).