public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/36566] Cannot bind packed field
       [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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: structurechart at yahoo dot com @ 2013-09-13  4:14 UTC (permalink / raw)
  To: gcc-bugs

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

Hei <structurechart at yahoo dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |structurechart at yahoo dot com

--- Comment #5 from Hei <structurechart at yahoo dot com> ---
any update on this? I run into the same issue with gcc 4.6.x


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
       [not found] <bug-36566-4@http.gcc.gnu.org/bugzilla/>
  2013-09-13  4:14 ` [Bug c++/36566] Cannot bind packed field structurechart at yahoo dot com
@ 2013-09-14 23:08 ` gabriel at teuton dot org
  2015-03-18  0:53 ` xiaoj at google dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: gabriel at teuton dot org @ 2013-09-14 23:08 UTC (permalink / raw)
  To: gcc-bugs

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 :-)]


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
       [not found] <bug-36566-4@http.gcc.gnu.org/bugzilla/>
  2013-09-13  4:14 ` [Bug c++/36566] Cannot bind packed field structurechart at yahoo dot com
  2013-09-14 23:08 ` gabriel at teuton dot org
@ 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
  4 siblings, 0 replies; 9+ messages in thread
From: xiaoj at google dot com @ 2015-03-18  0:53 UTC (permalink / raw)
  To: gcc-bugs

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

Xiao Jia <xiaoj at google dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xiaoj at google dot com

--- Comment #7 from Xiao Jia <xiaoj at google dot com> ---
What is the status of this?

Adding "const" makes it compile.  Is this the intended behavior or not?


struct Squeeze
{
    short   s;
} __attribute__((aligned(1), packed));

void VerticallyChallenged(const short&) {}

int main()
{
    Squeeze oj;
    const short& pit(oj.s);
    VerticallyChallenged(pit);
    VerticallyChallenged(oj.s);
}


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
       [not found] <bug-36566-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  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
  4 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2015-03-18  1:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Xiao Jia from comment #7)
> Adding "const" makes it compile.  Is this the intended behavior or not?

Yes, of course. A const-reference causes a temporary to be created, you didn't
bind to the packed field:

    Squeeze oj;
    oj.s = 0;
    const short& pit(oj.s);
    oj.s = 1;
    printf("%d %d", oj.s, pit);

This shows that oj.s and pit are not the same variable.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
       [not found] <bug-36566-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2015-03-18  1:56 ` redi at gcc dot gnu.org
@ 2021-12-31 15:11 ` steve+gcc at tecwec dot eu
  4 siblings, 0 replies; 9+ messages in thread
From: steve+gcc at tecwec dot eu @ 2021-12-31 15:11 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Estievenart <steve+gcc at tecwec dot eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |steve+gcc at tecwec dot eu

--- Comment #14 from Eric Estievenart <steve+gcc at tecwec dot eu> ---
What about:
struct S
{
        int i;
}  __attribute__((__packed__)) __attribute((aligned(8)));

void f( S* s )
{
        int& i = s->i; // Error here
}

S::i is obviously properly aligned, the compiler should know it and accept the
referencing.

As for the argument 'the field might be unaligned and this would violate... and
crash...', I don´t really agree. The compiler knows the arch, the alignment,
and it can decide:
- to emit an error if unaligned(or unknown) if alignment required by arch
- to emit a warning on other arch

Note by the way that this seems accepted by clang...

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
  2008-06-18 18:28 [Bug c++/36566] New: " nevin at eviloverlord dot com
                   ` (2 preceding siblings ...)
  2008-06-18 19:06 ` nevin at eviloverlord dot com
@ 2009-10-11  4:38 ` gabriel at teuton dot org
  3 siblings, 0 replies; 9+ messages in thread
From: gabriel at teuton dot org @ 2009-10-11  4:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from gabriel at teuton dot org  2009-10-11 04:38 -------
I have the same problem with g++ 4.2.4 and 4.3.2.


-- 

gabriel at teuton dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gabriel at teuton dot org


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
  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
  3 siblings, 0 replies; 9+ messages in thread
From: nevin at eviloverlord dot com @ 2008-06-18 19:06 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 689 bytes --]



------- Comment #3 from nevin at eviloverlord dot com  2008-06-18 19:06 -------
Expanding on my last comment:  which lines in the following code should fail to
compile:

struct Squeeze
{
    short   s;
} __attribute__((aligned(1), packed));

void VerticallyChallenged(short*) {}
void VerticallyChallenged(short&) {}

int main()
{
    Squeeze oj;
    short& pit(oj.s);
    short* ppit(&oj.s);

    VerticallyChallenged(ppit);     // okay
    VerticallyChallenged(&oj.s);    // okay

    VerticallyChallenged(pit);      // okay
    VerticallyChallenged(oj.s);     // cannot bind packed field ‘oj.Squeeze::s’
to ‘short int&’
}


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
  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
  3 siblings, 0 replies; 9+ messages in thread
From: nevin at eviloverlord dot com @ 2008-06-18 18:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from nevin at eviloverlord dot com  2008-06-18 18:49 -------
Why is this an error (I couldn't find anything in the documentation)?

Also, if pointers are used instead of references, should that be an error
(currently that compiles just fine)?


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/36566] Cannot bind packed field
  2008-06-18 18:28 [Bug c++/36566] New: " nevin at eviloverlord dot com
@ 2008-06-18 18:31 ` pinskia at gcc dot gnu dot org
  2008-06-18 18:49 ` nevin at eviloverlord dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-06-18 18:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2008-06-18 18:31 -------
This is correct you cannot take the address of a field of a packed struct.

>    short& pit(oj.s);

This should be also an error.


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-12-31 15:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-36566-4@http.gcc.gnu.org/bugzilla/>
2013-09-13  4:14 ` [Bug c++/36566] Cannot bind packed field structurechart at yahoo dot com
2013-09-14 23:08 ` gabriel at teuton dot org
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

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).