This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c/8132: Union of struct of union of struct bit field


>Number:         8132
>Category:       c
>Synopsis:       Union of struct of union of struct bit field
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Oct 03 03:36:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Philippe Trottier / Invers Oy
>Release:        gcc version 3.2(i686-linux) & 3.1 Apple  20020420 (prerelease)
>Organization:
>Environment:
powerpc-Apple-macosx (Jag 10.2.1) 
i686-lfs(4.0rc1)-linux 
>Description:
Bit field struct don't pack bit properly(?) when they are in a Union included in a struct. I might be stupid and not know how to do that too...
>How-To-Repeat:
#include<stdio.h>
typedef union 
{
    struct
    {
        union 
        {
            struct
            {
                int reserved:16;
                int R:15;
            }Tx;
            struct
            {
                int R:15;
                int PPS:1;
                int I:15;
            }Rx;
        }value;
        int TxRx:1;
    }sample;
    unsigned int Raw;
}gumpData;

typedef union 
{
    struct
    {
        union 
        {
            struct
            {
                int reserved:16;
                int R:15;
                int TxRx:1;
            }Tx;
            struct
            {
                int R:15;
                int PPS:1;
                int I:15;
                int TxRx:1;
            }Rx;
        }value;
    }sample;
    unsigned int Raw;
}gumpData2;

int main(void)
{
gumpData data;
gumpData2 data2;

data2.sample.value.Rx.TxRx=0;
data2.sample.value.Rx.I=12;
data2.sample.value.Rx.R=11;
data2.sample.value.Rx.PPS=0;

data.sample.TxRx=0;
data.sample.value.Rx.I=12;
data.sample.value.Rx.R=11;
data.sample.value.Rx.PPS=0;


printf("Data  %x Size: %d\n",data.Raw,sizeof(data));
printf("Data2 %x Size: %d\n",data2.Raw,sizeof(data2));
return(0);
}
$ gcc test.c
$ ./a.out
Data  800c000b Size: 8
Data2 c000b Size: 4
>Fix:
Workaround : include the bitfield in both union.
>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]