size of bitfieds / strange warning

Discussion in 'C++' started by stip, Feb 1, 2011.

  1. stip

    stip Guest

    I'm trying to use a struct type like the following:

    struct Compact
    {
    unsigned short int j : 7;
    unsigned long k : 56;
    unsigned char type : 1;
    };

    Why does the compiler complain about the second component? On my
    system long has 8 bytes, thus 56 bits should fit into it. But the
    compiler complains:

    warning: declared size for bit field is larger than the size of the
    bit field type; truncated to 32 bits

    How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    maximal single bitfield component 32 bits?
     
    stip, Feb 1, 2011
    #1
    1. Advertising

  2. * stip, on 01.02.2011 11:05:
    > I'm trying to use a struct type like the following:
    >
    > struct Compact
    > {
    > unsigned short int j : 7;
    > unsigned long k : 56;
    > unsigned char type : 1;
    > };
    >
    > Why does the compiler complain about the second component? On my
    > system long has 8 bytes, thus 56 bits should fit into it.


    This seems to be your opinion.


    > But the
    > compiler complains:
    >
    > warning: declared size for bit field is larger than the size of the
    > bit field type; truncated to 32 bits


    This seems to be reality.


    > How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    > maximal single bitfield component 32 bits?


    Use C++0x 'long long' type.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Feb 1, 2011
    #2
    1. Advertising

  3. stip

    stip Guest

    On 1 Feb., 12:03, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * stip, on 01.02.2011 11:05:
    >
    > > I'm trying to use a struct type like the following:

    >
    > > struct Compact
    > > {
    > >      unsigned short int j : 7;
    > >      unsigned long      k : 56;
    > >      unsigned char   type : 1;
    > > };

    >
    > > Why does the compiler complain about the second component? On my
    > > system long has 8 bytes, thus 56 bits should fit into it.

    >
    > This seems to be your opinion.


    It is my opinion and sizeof(long) yields 8, thus it's a tested
    opinion.

    >
    > > But the
    > > compiler complains:

    >
    > > warning: declared size for bit field is larger than the size of the
    > > bit field type; truncated to 32 bits

    >
    > This seems to be reality.


    And what's the reason for this reality?

    >
    > > How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    > > maximal single bitfield component 32 bits?

    >
    > Use C++0x 'long long' type.


    This doesn't change the result.

    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>
     
    stip, Feb 1, 2011
    #3
  4. On Feb 1, 1:05 pm, stip <> wrote:
    > I'm trying to use a struct type like the following:
    >
    > struct Compact
    > {
    >     unsigned short int j : 7;
    >     unsigned long      k : 56;
    >     unsigned char   type : 1;
    >
    > };
    >
    > Why does the compiler complain about the second component? On my
    > system long has 8 bytes, thus 56 bits should fit into it. But the
    > compiler complains:
    >
    > warning: declared size for bit field is larger than the size of the
    > bit field type; truncated to 32 bits
    >
    > How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    > maximal single bitfield component 32 bits?


    Hi

    I tested your program with GCC 4.5.0. Because, the size of long
    is equal to size of int, so, I used the type long long, which
    was added to C++0x. Your program is OK. No error, no warning.
    Which software/hardware platform and programming environment
    you use?

    Regards,
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Feb 1, 2011
    #4
  5. stip

    stip Guest

    On 1 Feb., 12:34, Saeed Amrollahi <> wrote:
    > On Feb 1, 1:05 pm, stip <> wrote:
    >
    >
    >
    >
    >
    > > I'm trying to use a struct type like the following:

    >
    > > struct Compact
    > > {
    > >     unsigned short int j : 7;
    > >     unsigned long      k : 56;
    > >     unsigned char   type : 1;

    >
    > > };

    >
    > > Why does the compiler complain about the second component? On my
    > > system long has 8 bytes, thus 56 bits should fit into it. But the
    > > compiler complains:

    >
    > > warning: declared size for bit field is larger than the size of the
    > > bit field type; truncated to 32 bits

    >
    > > How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    > > maximal single bitfield component 32 bits?

    >
    > Hi
    >
    > I tested your program with GCC 4.5.0. Because, the size of long
    > is equal to size of int, so, I used the type long long, which
    > was added to C++0x. Your program is OK. No error, no warning.
    > Which software/hardware platform and programming environment
    > you use?
    >
    > Regards,
    >   -- Saeed Amrollahi


    Thanks for checking this.
    If I use long long instead of long I get no warning too.
    Nevertheless the value of Compact.k is truncated to 32 bits. It's not
    just the warning, the problem is the behaviour of the program.

    Regards,
    Alex
     
    stip, Feb 1, 2011
    #5
  6. * stip, on 01.02.2011 12:24:
    > On 1 Feb., 12:03, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >> * stip, on 01.02.2011 11:05:
    >>
    >>> I'm trying to use a struct type like the following:

    >>
    >>> struct Compact
    >>> {
    >>> unsigned short int j : 7;
    >>> unsigned long k : 56;
    >>> unsigned char type : 1;
    >>> };

    >>
    >>> Why does the compiler complain about the second component? On my
    >>> system long has 8 bytes, thus 56 bits should fit into it.

    >>
    >> This seems to be your opinion.

    >
    > It is my opinion and sizeof(long) yields 8, thus it's a tested
    > opinion.
    >
    >>
    >>> But the
    >>> compiler complains:

    >>
    >>> warning: declared size for bit field is larger than the size of the
    >>> bit field type; truncated to 32 bits

    >>
    >> This seems to be reality.

    >
    > And what's the reason for this reality?
    >
    >>
    >>> How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    >>> maximal single bitfield component 32 bits?

    >>
    >> Use C++0x 'long long' type.

    >
    > This doesn't change the result.


    Else-thread you're saying that it does get rid of the warning.

    And you fail to specify which compiler.

    You sound confused, possibly trolling.


    - Alf


    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Feb 1, 2011
    #6
  7. stip

    stip Guest

    On 1 Feb., 14:31, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * stip, on 01.02.2011 12:24:
    >
    >
    >
    >
    >
    > > On 1 Feb., 12:03, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > >  wrote:
    > >> * stip, on 01.02.2011 11:05:

    >
    > >>> I'm trying to use a struct type like the following:

    >
    > >>> struct Compact
    > >>> {
    > >>>       unsigned short int j : 7;
    > >>>       unsigned long      k : 56;
    > >>>       unsigned char   type : 1;
    > >>> };

    >
    > >>> Why does the compiler complain about the second component? On my
    > >>> system long has 8 bytes, thus 56 bits should fit into it.

    >
    > >> This seems to be your opinion.

    >
    > > It is my opinion and sizeof(long) yields 8, thus it's a tested
    > > opinion.

    >
    > >>> But the
    > >>> compiler complains:

    >
    > >>> warning: declared size for bit field is larger than the size of the
    > >>> bit field type; truncated to 32 bits

    >
    > >> This seems to be reality.

    >
    > > And what's the reason for this reality?

    >
    > >>> How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    > >>> maximal single bitfield component 32 bits?

    >
    > >> Use C++0x 'long long' type.

    >
    > > This doesn't change the result.

    >
    > Else-thread you're saying that it does get rid of the warning.
    >
    > And you fail to specify which compiler.
    >
    > You sound confused, possibly trolling.
    >
    > - Alf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>


    Re-Read your first answer to my question and you find yourself
    possibly trolling. Instead of trying to help, you just give answers,
    well to say it positive, which are not helpful at all.
    I said I got rid of the warning by replacing long by long long, but
    the value was still truncated. So the focus changed (independent of
    the compiler version). But you're right, indeed I forgot to mention
    the compiler. And the behavior is not specific to just one compiler
    version but occurs for gcc (4.0, 4.4.2), icc (11.1).
    In the meanwhile another question seems much more important to me,
    i.e. if the code is at all legal. Because the struct inside the union
    is anonymous. I fear that's not in the standard. The compilers I used
    do not complain, but that's not satisfying as answer.

    Alex
     
    stip, Feb 1, 2011
    #7
  8. * stip, on 01.02.2011 15:04:
    > On 1 Feb., 14:31, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >> * stip, on 01.02.2011 12:24:
    >>
    >>
    >>
    >>
    >>
    >>> On 1 Feb., 12:03, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    >>> > wrote:
    >>>> * stip, on 01.02.2011 11:05:

    >>
    >>>>> I'm trying to use a struct type like the following:

    >>
    >>>>> struct Compact
    >>>>> {
    >>>>> unsigned short int j : 7;
    >>>>> unsigned long k : 56;
    >>>>> unsigned char type : 1;
    >>>>> };

    >>
    >>>>> Why does the compiler complain about the second component? On my
    >>>>> system long has 8 bytes, thus 56 bits should fit into it.

    >>
    >>>> This seems to be your opinion.

    >>
    >>> It is my opinion and sizeof(long) yields 8, thus it's a tested
    >>> opinion.

    >>
    >>>>> But the
    >>>>> compiler complains:

    >>
    >>>>> warning: declared size for bit field is larger than the size of the
    >>>>> bit field type; truncated to 32 bits

    >>
    >>>> This seems to be reality.

    >>
    >>> And what's the reason for this reality?

    >>
    >>>>> How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    >>>>> maximal single bitfield component 32 bits?

    >>
    >>>> Use C++0x 'long long' type.

    >>
    >>> This doesn't change the result.

    >>
    >> Else-thread you're saying that it does get rid of the warning.
    >>
    >> And you fail to specify which compiler.
    >>
    >> You sound confused, possibly trolling.

    >

    [snip quoted signature]
    [snip

    > Re-Read your first answer to my question and you find yourself
    > possibly trolling. Instead of trying to help, you just give answers,


    Are you, by any chance, seriously sleep-deprived?

    You **do not** want answers?


    > well to say it positive, which are not helpful at all.


    So using 'long long' didn't help at all?

    But it did get rid of your warning?

    Again, are you seriously sleep-deprived?


    > I said I got rid of the warning by replacing long by long long, but
    > the value was still truncated. So the focus changed (independent of
    > the compiler version). But you're right, indeed I forgot to mention
    > the compiler. And the behavior is not specific to just one compiler
    > version but occurs for gcc (4.0, 4.4.2), icc (11.1).


    Here you forget to mention which OS.

    For example, "long" is 32-bits for gcc under Windows, while you are claiming
    that with your compiler it's 64-bits.

    Are you sleep-deprived?


    > In the meanwhile another question seems much more important to me,
    > i.e. if the code is at all legal. Because the struct inside the union
    > is anonymous.


    You haven't mentioned any union before now. First we should guess compiler, now
    we should guess the OS, and finally we'll be left guessing at the code that's
    wrong, the code you haven't shown and that is in error.

    Note well:


    *** Readers are NOT telepaths ***


    Readers of your postings, like me, can not actually "see" the code in front of you.

    How about some sleep?


    > I fear that's not in the standard. The compilers I used
    > do not complain, but that's not satisfying as answer.


    Should be OK, but can't say without the code.

    Here's code that should work with any compiler that supports "long long":


    struct Compact
    {
    unsigned short j : 7;
    unsigned long long k : 56;
    unsigned char type : 1;
    };

    #include <iostream>
    using namespace std;
    int main()
    {
    Compact o = { 101, 1ull << 55, 1 };

    cout << hex;
    cout << o.j << "\n" << o.k << "\n" << o.type+0 << "\n";
    }


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Feb 1, 2011
    #8
  9. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Saeed Amrollahi wrote:

    > On Feb 1, 1:05 pm, stip <> wrote:
    >> I'm trying to use a struct type like the following:
    >>
    >> struct Compact
    >> {
    >> unsigned short int j : 7;
    >> unsigned long k : 56;
    >> unsigned char type : 1;
    >>
    >> };
    >>
    >> Why does the compiler complain about the second component? On my
    >> system long has 8 bytes, thus 56 bits should fit into it. But the
    >> compiler complains:
    >>
    >> warning: declared size for bit field is larger than the size of the
    >> bit field type; truncated to 32 bits
    >>
    >> How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    >> maximal single bitfield component 32 bits?

    >
    > Hi
    >
    > I tested your program with GCC 4.5.0. Because, the size of long
    > is equal to size of int, so, I used the type long long, which
    > was added to C++0x. Your program is OK. No error, no warning.
    > Which software/hardware platform and programming environment
    > you use?


    A GCC long is 64-bit on 64-bit systems but 32-bit on 32-bit systems
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.10 (GNU/Linux)

    iEYEARECAAYFAk1IK0AACgkQZ1Turg5KUCmYcACfQeaig+4MgSxpS1zHilQBUYbS
    6lgAoIyTenNVRSko8UhZwKSYDVU5Z2Rc
    =AA0O
    -----END PGP SIGNATURE-----
     
    Michael Tsang, Feb 1, 2011
    #9
  10. stip

    Paul Guest

    "Alf P. Steinbach /Usenet" <> wrote in
    message news:ii9729$ie4$-september.org...
    >* stip, on 01.02.2011 15:04:
    >> On 1 Feb., 14:31, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    >> > wrote:
    >>> * stip, on 01.02.2011 12:24:

    <snip>
    Alf you are being a complete arsehole.
    As usual :)
     
    Paul, Feb 1, 2011
    #10
  11. Am 01.02.2011 16:48, schrieb Michael Tsang:
    >
    > A GCC long is 64-bit on 64-bit systems but 32-bit on 32-bit systems


    IMHO it depends on the OS: Windows 64 is LLP64 whereas Linux/Unix is
    LP64 (http://en.wikipedia.org/wiki/LLP64)

    Lars
     
    Lars Tetzlaff, Feb 1, 2011
    #11
  12. stip

    Jorgen Grahn Guest

    On Tue, 2011-02-01, stip wrote:
    > I'm trying to use a struct type like the following:
    >
    > struct Compact
    > {
    > unsigned short int j : 7;
    > unsigned long k : 56;
    > unsigned char type : 1;
    > };
    >
    > Why does the compiler complain about the second component? On my
    > system long has 8 bytes, thus 56 bits should fit into it. But the
    > compiler complains:
    >
    > warning: declared size for bit field is larger than the size of the
    > bit field type; truncated to 32 bits
    >
    > How can I tell the compiler to allow 64 bits, i.e. long? Or is the
    > maximal single bitfield component 32 bits?


    Bitfields make me nervous. I haven't used them for ages.

    Since 7+56+1 == 64 I suspect this maps to an uint64_t somewhere in
    your design. I suggest just wrapping a uint64_t in struct Compact and
    provide member functions to mess with the three parts as needed. I bet
    that would be just as fast with inlining and all, and almost as easy
    to use. And, it works ;-)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 1, 2011
    #12
  13. stip

    James Kanze Guest

    On Feb 1, 7:19 pm, Lars Tetzlaff <> wrote:
    > Am 01.02.2011 16:48, schrieb Michael Tsang:
    > > A GCC long is 64-bit on 64-bit systems but 32-bit on 32-bit systems


    > IMHO it depends on the OS: Windows 64 is LLP64 whereas Linux/Unix is
    > LP64 (http://en.wikipedia.org/wiki/LLP64)


    The final choice is in the compiler; it decides how it will lay
    something out. In practice, however, no compiler will go
    against the layouts (sizes, padding, etc.) used by the OS in its
    API, since otherwise, you couldn't use the API in code compiled
    by the compiler.

    --
    James Kanze
     
    James Kanze, Feb 2, 2011
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Becker
    Replies:
    0
    Views:
    1,379
    Pete Becker
    Feb 10, 2005
  2. B. Williams

    warning C4267 and warning C4996

    B. Williams, Oct 26, 2006, in forum: C++
    Replies:
    17
    Views:
    2,635
  3. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,587
    Michael Jung
    May 25, 2008
  4. WARNING! Prosoftstore.com is a SCAM! WARNING!

    , Jul 8, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    319
  5. Julian Mehnle
    Replies:
    17
    Views:
    886
    Julian Mehnle
    May 18, 2006
Loading...

Share This Page