granularity of size of operator

Discussion in 'C Programming' started by chinu, Aug 4, 2006.

  1. chinu

    chinu Guest

    mail
    hi all..

    is it possible to find the size of a structure upto bit size.., where
    the structure contains only one element and that too with bit
    specifier..eg..

    struct x {
    char a : 4;
    } y;

    int main(){

    printf("size of struct is %d\n",sizoef(y)); //will give 1
    }

    is there any way to get the size exactly upto no of bits.. so for the
    ex above.. size should be 4..., i can disable ths automatic stuffing by
    pragma.. still the smallest granularity for pragma is 1 byte only..
    -- i think compiler should be able to tell this thing.., it is just
    matter of asking the compiler right thing.. he wh
     
    chinu, Aug 4, 2006
    #1
    1. Advertising

  2. chinu

    jacob navia Guest

    chinu a écrit :
    > mail
    > hi all..
    >
    > is it possible to find the size of a structure upto bit size.., where
    > the structure contains only one element and that too with bit
    > specifier..eg..
    >
    > struct x {
    > char a : 4;
    > } y;
    >
    > int main(){
    >
    > printf("size of struct is %d\n",sizoef(y)); //will give 1
    > }
    >
    > is there any way to get the size exactly upto no of bits.. so for the
    > ex above.. size should be 4..., i can disable ths automatic stuffing by
    > pragma.. still the smallest granularity for pragma is 1 byte only..
    > -- i think compiler should be able to tell this thing.., it is just
    > matter of asking the compiler right thing.. he wh
    >


    The processor you use can address bits?

    The smallest addressable unit in most processors I know
    is 1 byte. Period.

    Since you can't address less, there is no point in having sizeof
    return bit sizes.
     
    jacob navia, Aug 4, 2006
    #2
    1. Advertising

  3. "chinu" <> writes:
    > is it possible to find the size of a structure upto bit size.., where
    > the structure contains only one element and that too with bit
    > specifier..eg..
    >
    > struct x {
    > char a : 4;
    > } y;
    >
    > int main(){
    >
    > printf("size of struct is %d\n",sizoef(y)); //will give 1
    > }
    >
    > is there any way to get the size exactly upto no of bits.. so for the
    > ex above.. size should be 4..., i can disable ths automatic stuffing by
    > pragma.. still the smallest granularity for pragma is 1 byte only..
    > -- i think compiler should be able to tell this thing.., it is just
    > matter of asking the compiler right thing.. he wh


    The size of any object (other than a bit field) is always a whole
    number of bytes.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 4, 2006
    #3
  4. chinu

    Guest

    jacob navia wrote:
    > chinu a écrit :
    > > mail
    > > hi all..
    > >
    > > is it possible to find the size of a structure upto bit size.., where
    > > the structure contains only one element and that too with bit
    > > specifier..eg..
    > >
    > > struct x {
    > > char a : 4;
    > > } y;
    > >
    > > int main(){
    > >
    > > printf("size of struct is %d\n",sizoef(y)); //will give 1
    > > }
    > >
    > > is there any way to get the size exactly upto no of bits.. so for the
    > > ex above.. size should be 4..., i can disable ths automatic stuffing by
    > > pragma.. still the smallest granularity for pragma is 1 byte only..
    > > -- i think compiler should be able to tell this thing.., it is just
    > > matter of asking the compiler right thing.. he wh
    > >

    >
    > The processor you use can address bits?
    >
    > The smallest addressable unit in most processors I know
    > is 1 byte. Period.


    The Microchip PIC can address bits and most PIC C compilers have
    extensions to do bit addressing though not the way the OP's written it.
    But yeah, no implementation of C I know can malloc or sizeof bits.
     
    , Aug 4, 2006
    #4
  5. chinu

    chinu Guest

    jacob navia wrote:

    > chinu a écrit :
    > > mail
    > > hi all..
    > >
    > > is it possible to find the size of a structure upto bit size.., where
    > > the structure contains only one element and that too with bit
    > > specifier..eg..
    > >
    > > struct x {
    > > char a : 4;
    > > } y;
    > >
    > > int main(){
    > >
    > > printf("size of struct is %d\n",sizoef(y)); //will give 1
    > > }
    > >
    > > is there any way to get the size exactly upto no of bits.. so for the
    > > ex above.. size should be 4..., i can disable ths automatic stuffing by
    > > pragma.. still the smallest granularity for pragma is 1 byte only..
    > > -- i think compiler should be able to tell this thing.., it is just
    > > matter of asking the compiler right thing.. he wh
    > >

    >
    > The processor you use can address bits?
    >

    no, its not about addressing the bits individually.

    > The smallest addressable unit in most processors I know
    > is 1 byte. Period.
    >

    same is true for me..:))
    > Since you can't address less, there is no point in having sizeof


    point comes in different context.. it would be helpful in embedded
    system env.. where i need to know the size of individual bits to work
    on them only.., othervise i need to remember the width of each field..



    > return bit sizes.
     
    chinu, Aug 4, 2006
    #5
  6. chinu

    Richard Guest

    jacob navia <> writes:

    > chinu a écrit :
    >> mail
    >> hi all..
    >> is it possible to find the size of a structure upto bit size.., where
    >> the structure contains only one element and that too with bit
    >> specifier..eg..
    >> struct x {
    >> char a : 4;
    >> } y;
    >> int main(){
    >> printf("size of struct is %d\n",sizoef(y)); //will give 1
    >> }
    >> is there any way to get the size exactly upto no of bits.. so for the
    >> ex above.. size should be 4..., i can disable ths automatic stuffing by
    >> pragma.. still the smallest granularity for pragma is 1 byte only..
    >> -- i think compiler should be able to tell this thing.., it is just
    >> matter of asking the compiler right thing.. he wh
    >>

    >
    > The processor you use can address bits?
    >


    He didnt ask to address the bits. He asked to find the size in bits of a bitfield.
     
    Richard, Aug 4, 2006
    #6
  7. chinu

    goose Guest

    chinu wrote:
    > jacob navia wrote:
    >


    <snipped>

    > > The processor you use can address bits?
    > >

    > no, its not about addressing the bits individually.


    I'd be interested to know what you plan to do with
    the size in bits.

    >
    > > The smallest addressable unit in most processors I know
    > > is 1 byte. Period.
    > >

    > same is true for me..:))
    > > Since you can't address less, there is no point in having sizeof

    >
    > point comes in different context.. it would be helpful in embedded
    > system env.. where i need to know the size of individual bits to work
    > on them only.., othervise i need to remember the width of each field..


    I *am* in embedded environments, but I have yet
    yearn for a sizeof that returns the number
    of bits of a type or variable.

    Anyway, since the standard defines sizeof (char) to be
    1, what do you expect to get for a datatype less than
    1 byte in size? 0.5?

    goose,
     
    goose, Aug 4, 2006
    #7
  8. chinu wrote:
    > mail
    > hi all..
    >
    > is it possible to find the size of a structure upto bit size.., where
    > the structure contains only one element and that too with bit
    > specifier..eg..
    >
    > struct x {
    > char a : 4;
    > } y;
    >
    > int main(){
    >
    > printf("size of struct is %d\n",sizoef(y)); //will give 1
    > }


    #include <stdio.h>
    #include <limits.h>

    struct x
    {
    unsigned a:4; /* declaring a bit field as type char
    is not part of C */
    } y;

    int main()
    {
    printf("[output for this implementation]\n"
    "size of struct is %u bytes, %u bits\n",
    (unsigned) sizeof y, (unsigned) (CHAR_BIT * sizeof y));
    return 0;
    }

    [output for this implementation]
    size of struct is 4 bytes, 32 bits

    > is there any way to get the size exactly upto no of bits.. so for the
    > ex above.. size should be 4...,


    Where did you get such a silly idea? You will note that your comment
    "//will give 1" is also untrue for the implementation on which I ran the
    above program.

    > i can disable ths automatic stuffing by
    > pragma.. still the smallest granularity for pragma is 1 byte only..


    In what units are addresses specified on your machine? Can you really
    expect an array of 'struct x's to be properly aligned for access _and_
    have each one only 4 bits long? Actually, those are rhetorical
    questions, since a C program treats the smallest addressable unit as a
    char, which must be at least 8 bits wide, no matter what the underlying
    hardware might do.

    > -- i think compiler should be able to tell this thing..,


    Tell what thing? Your mental states are not available to the compiler.
    The size of that struct in either bytes or bits is not a reflection of
    your mental state, and the compiler should not have to probe your brain
    to find the answers.

    > it is just
    > matter of asking the compiler right thing.. he wh


    It is just a matter of knowing what questions make sense to ask and what
    expectations it makes sense to hold.
     
    Martin Ambuhl, Aug 4, 2006
    #8
  9. Martin Ambuhl wrote:
    > chinu wrote:


    [...]

    > > is it possible to find the size of a structure upto bit size.., where
    > > the structure contains only one element and that too with bit
    > > specifier..eg..


    I think what OP wants is a way to know how many bits a field _pretends_
    to span _not_ what the actual size of the structure or the fields is.

    > > is there any way to get the size exactly upto no of bits.. so for the
    > > ex above.. size should be 4...,

    >
    > Where did you get such a silly idea? You will note that your comment
    > "//will give 1" is also untrue for the implementation on which I ran the
    > above program.


    I don't think an idea is silly or stupid, it's either plausible or not
    by currently available means but definitely not silly. Infact the
    compiler should be able to tell how many bits a bit field represents,
    whether it's useful or not shouldn't bother any one but the one who
    requires this information.
     
    Haroon Shafiq, Aug 4, 2006
    #9
  10. chinu

    Eric Sosman Guest

    chinu wrote:
    >>>
    >>>is it possible to find the size of a structure upto bit size.., where
    >>>the structure contains only one element and that too with bit
    >>>specifier..eg..
    >>>
    >>>struct x {
    >>> char a : 4;


    A cautionary nit-pick: The only portable "base types" for
    bit-fields are signed and unsigned `int', plus `_Bool' in C99.
    The compiler is allowed to accept other types like `char' if
    it wishes, but the next compiler you use is allowed to reject
    them.

    >>>} y;
    >>>

    > [... and in a follow-up:]
    >
    > point comes in different context.. it would be helpful in embedded
    > system env.. where i need to know the size of individual bits to work
    > on them only.., othervise i need to remember the width of each field..


    Why do you "need to remember the width" when the compiler
    is already remembering it for you? If you write `y.a = 3' the
    compiler will figure out which bits to set and clear, and where
    they're located within `y'; you don't need to remember anything
    except that `y' contains an `a' of integer type. What else do
    you need to remember, and why?

    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 4, 2006
    #10
  11. chinu wrote:
    > mail
    > hi all..
    >
    > is it possible to find the size of a structure upto bit size.., where
    > the structure contains only one element and that too with bit
    > specifier..eg..
    >
    > struct x {
    > char a : 4;
    > } y;
    >
    > int main(){
    >
    > printf("size of struct is %d\n",sizoef(y)); //will give 1
    > }



    probably not doable at compile-time. You have a better chance of doing
    it at run-time by doing a game of twenty questions, something like:

    unsigned long int i; struct y q;

    for( i=q.a=1; i == q.a; i <<= 1,q.a <<= 1 ) ;

    printf("q.a has %d bits\n", i );


    .... then again, there's no guarantee, the compiler might see there's
    nothing else in that byte and not bother doing the masking. Or the
    signedness of a may break the comparison.

    I'e always wanted a listing option where the compiler would explicitly
    list out how it packed structs, for those cases where you need a C
    struct to exactly match some OS structure, or other language structure.
    For example, Fortran array descriptors have a word full of important
    flag bits, it would be nice to be able to access them without a lot of
    folderol. Or PC parallel port registers, which are a maze of about 28
    packed bit fields.
    It's usually done by a whole gob of #define FIFO_RESET_SHIFT 13 and
    #define FIFO_RESET_MASK 0x7, but a nice packed struct would be awhole
    lot cleaner to use.
    I know, non-portable.
     
    Ancient_Hacker, Aug 4, 2006
    #11
  12. "chinu" <> writes:
    > is it possible to find the size of a structure upto bit size.., where
    > the structure contains only one element and that too with bit
    > specifier..eg..
    >
    > struct x {
    > char a : 4;
    > } y;
    >
    > int main(){
    >
    > printf("size of struct is %d\n",sizoef(y)); //will give 1
    > }
    >
    > is there any way to get the size exactly upto no of bits.. so for the
    > ex above.. size should be 4..., i can disable ths automatic stuffing by
    > pragma.. still the smallest granularity for pragma is 1 byte only..
    > -- i think compiler should be able to tell this thing.., it is just
    > matter of asking the compiler right thing.. he wh


    You can always remember it yourself:

    #define A_SIZE 4
    struct x {
    unsigned int a : A_SIZE;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 4, 2006
    #12
    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. Manish_Ganvir
    Replies:
    13
    Views:
    1,634
    Keith Thompson
    Feb 14, 2005
  2. padding granularity

    , Jun 27, 2005, in forum: C Programming
    Replies:
    1
    Views:
    334
    Lawrence Kirby
    Jun 27, 2005
  3. kj

    Granularity of OSError

    kj, Sep 18, 2009, in forum: Python
    Replies:
    12
    Views:
    545
  4. James Snell

    SocketPermission granularity in .config files

    James Snell, Jun 21, 2008, in forum: ASP .Net Security
    Replies:
    0
    Views:
    112
    James Snell
    Jun 21, 2008
  5. Peter Bailey

    Time granularity

    Peter Bailey, Feb 27, 2007, in forum: Ruby
    Replies:
    5
    Views:
    149
    Peter Bailey
    Feb 28, 2007
Loading...

Share This Page