structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ranjeet.gupta@gmail.com

    #1

    structure

    Dear All !!

    Before i qoute my querry, I will like to qoute my analysis and my
    Knowledge

    Struct a {
    int raw;
    char data;
    };

    the size of above struct is 8 byte ... correct (I am talking of
    32 bit procesor)

    Struct a {
    int raw;
    char data_1;
    char data_2;
    };

    again the size of above structure is 8 bytes.

    Means the structure of padding is going on... right...

    Now suppose I have to specefically allocate the 4 bits,
    then What i will do is

    strcut a {

    cahr a:4;
    }

    now ideally speaking i should get the size of the above struct as
    4 bits, But infact i will get the 1 byte, Again i run in the situation
    that i dont want to allocate the extra 4 bits (which by default
    structure is doing padding),

    now my question comes.

    First
    how I am going to allocate the 4 bits only,

    second (not directly realated to the above)
    how i will assure that structure is alligned to the machine lenght ?
    what i have to do in the structure that make me sure that it is
    alligned to the machine word lenght.

    I supposse that Strcuture padding is done to get the exectution of
    Arthematic execution fast.... m i correct as it is alligned
    to the machine length.

    Please let me know about the above querry

    Thanks In Advance
    Ranjeet

  • Mukul Pandey

    #2
    Re: structure

    1. by saying,

    char a:4;

    you are not just allocating 4 bits, instead, you are requesting
    for a bit-wise allocation. Suppose, you know that you have three
    attributes, with known ranges, you could say,

    struct bitwise_alloc
    {
    char a:4; (range 0 to 15)
    char b:2; (range 0 to 3)
    char c:1; (range 0 to 1)
    char d:1;
    };

    The size would still be 1 byte (if padding is not there). No allocation
    below 1 byte is possible (for an object/variable).

    2. you could do this using the compiler directive (#pragma pack),
    but then it might not be a generic implementation.

    Comment

    • Mohan

      #3
      Re: structure



      ranjeet.gupta@g mail.com wrote:[color=blue]
      > Dear All !!
      >
      > Before i qoute my querry, I will like to qoute my analysis and my
      > Knowledge
      >
      > Struct a {
      > int raw;
      > char data;
      > };
      >
      > the size of above struct is 8 byte ... correct (I am talking of
      > 32 bit procesor)
      >
      > Struct a {
      > int raw;
      > char data_1;
      > char data_2;
      > };
      >
      > again the size of above structure is 8 bytes.
      >
      > Means the structure of padding is going on... right...
      >
      > Now suppose I have to specefically allocate the 4 bits,
      > then What i will do is
      >
      > strcut a {
      >
      > cahr a:4;
      > }[/color]
      bitfield are appropriate for *int*s
      [color=blue]
      >
      > now ideally speaking i should get the size of the above struct as
      > 4 bits, But infact i will get the 1 byte, Again i run in the situation
      > that i dont want to allocate the extra 4 bits (which by default
      > structure is doing padding),[/color]

      You can get size of a structure (infact any data type or variable)
      atleast in multiples of byte.

      [color=blue]
      >
      > now my question comes.
      >
      > First
      > how I am going to allocate the 4 bits only,[/color]
      I think, allocating a memory of 4bits is not possible. However you can
      specify __attribute__(( __packed__)) along with structure definition to
      pack the allocation to byte aligned.
      [color=blue]
      >
      > second (not directly realated to the above)
      > how i will assure that structure is alligned to the machine lenght ?
      > what i have to do in the structure that make me sure that it is
      > alligned to the machine word lenght.[/color]

      __attribute__ ((__aligned__)) might help you.

      --
      Mohan
      [color=blue]
      >
      > I supposse that Strcuture padding is done to get the exectution of
      > Arthematic execution fast.... m i correct as it is alligned
      > to the machine length.
      >
      > Please let me know about the above querry
      >
      > Thanks In Advance
      > Ranjeet[/color]

      Comment

      • ranjeet.gupta@gmail.com

        #4
        Re: structure



        Mukul Pandey wrote:[color=blue]
        > 1. by saying,
        >
        > char a:4;
        >
        > you are not just allocating 4 bits, instead, you are requesting
        > for a bit-wise allocation. Suppose, you know that you have three
        > attributes, with known ranges, you could say,
        >
        > struct bitwise_alloc
        > {
        > char a:4; (range 0 to 15)
        > char b:2; (range 0 to 3)
        > char c:1; (range 0 to 1)
        > char d:1;
        > };
        >
        > The size would still be 1 byte (if padding is not there). No allocation
        > below 1 byte is possible (for an object/variable).[/color]

        yes you are correct what you are saying.
        [color=blue]
        >
        > 2. you could do this using the compiler directive (#pragma pack),
        > but then it might not be a generic implementation.[/color]

        It will be help me if you give me some pointer on this
        (Any web link / Pseduo code)

        Comment

        • ranjeet.gupta@gmail.com

          #5
          Re: structure



          Mohan wrote:[color=blue]
          > ranjeet.gupta@g mail.com wrote:[color=green]
          > > Dear All !!
          > >
          > > Before i qoute my querry, I will like to qoute my analysis and my
          > > Knowledge
          > >
          > > Struct a {
          > > int raw;
          > > char data;
          > > };
          > >
          > > the size of above struct is 8 byte ... correct (I am talking of
          > > 32 bit procesor)
          > >
          > > Struct a {
          > > int raw;
          > > char data_1;
          > > char data_2;
          > > };
          > >
          > > again the size of above structure is 8 bytes.
          > >
          > > Means the structure of padding is going on... right...
          > >
          > > Now suppose I have to specefically allocate the 4 bits,
          > > then What i will do is
          > >
          > > strcut a {
          > >
          > > cahr a:4;
          > > }[/color]
          > bitfield are appropriate for *int*s
          >[color=green]
          > >
          > > now ideally speaking i should get the size of the above struct as
          > > 4 bits, But infact i will get the 1 byte, Again i run in the situation
          > > that i dont want to allocate the extra 4 bits (which by default
          > > structure is doing padding),[/color]
          >
          > You can get size of a structure (infact any data type or variable)
          > atleast in multiples of byte.[/color]

          correct !![color=blue]
          >
          >[color=green]
          > >
          > > now my question comes.
          > >
          > > First
          > > how I am going to allocate the 4 bits only,[/color]
          > I think, allocating a memory of 4bits is not possible. However you can
          > specify __attribute__(( __packed__)) along with structure definition to
          > pack the allocation to byte aligned.[/color]

          any psedo code or link will be evry helpful to me.[color=blue]
          >[color=green]
          > >
          > > second (not directly realated to the above)
          > > how i will assure that structure is alligned to the machine lenght ?
          > > what i have to do in the structure that make me sure that it is
          > > alligned to the machine word lenght.[/color]
          >
          > __attribute__ ((__aligned__)) might help you.[/color]

          for this also any psedo code or link will be evry helpful to me.

          Thanks in advance[color=blue]
          >
          > --
          > Mohan
          >[color=green]
          > >
          > > I supposse that Strcuture padding is done to get the exectution of
          > > Arthematic execution fast.... m i correct as it is alligned
          > > to the machine length.
          > >
          > > Please let me know about the above querry
          > >
          > > Thanks In Advance
          > > Ranjeet[/color][/color]

          Comment

          • Lawrence Kirby

            #6
            Re: structure

            On Mon, 30 May 2005 03:40:55 -0700, ranjeet.gupta wrote:
            [color=blue]
            > Dear All !!
            >
            > Before i qoute my querry, I will like to qoute my analysis and my
            > Knowledge
            >
            > Struct a {
            > int raw;
            > char data;
            > };
            >
            > the size of above struct is 8 byte ... correct (I am talking of
            > 32 bit procesor)[/color]

            It may or may not be 8 bytes, even on a 32 bit processor, although
            it often is. An implementyation is allowed to make various choices
            over object size and alignment requirements.
            [color=blue]
            > Struct a {
            > int raw;
            > char data_1;
            > char data_2;
            > };
            >
            > again the size of above structure is 8 bytes.[/color]

            Maybe, maybe not.
            [color=blue]
            > Means the structure of padding is going on... right...[/color]

            That is typical.
            [color=blue]
            > Now suppose I have to specefically allocate the 4 bits,
            > then What i will do is
            >
            > strcut a {
            >
            > cahr a:4;
            > }
            >
            > now ideally speaking i should get the size of the above struct as
            > 4 bits, But infact i will get the 1 byte,[/color]

            Often you'll get the word size, perhaps 4 bytes on a 32bit architecture.
            [color=blue]
            > Again i run in the situation
            > that i dont want to allocate the extra 4 bits (which by default
            > structure is doing padding),[/color]

            It isn't padding in the same sense, there is an allocation unit that
            that compiler will use for bit-fields.
            [color=blue]
            > now my question comes.
            >
            > First
            > how I am going to allocate the 4 bits only,[/color]

            You can't do that directly using C's type mechanism, the size of
            any non-bitfield object in C must be an integral number of bytes. That
            includes any structure type such as struct a, even if it only contains
            bit-field members.

            The question is fairly meaningless for one bit-field in isolation, the
            issue only really arises if you want to place several small fields
            together. If you define several adjacent bit-fields in the structure the
            compiler will makereasonable efforts to pack them, but the structure as a
            while will still be an integral number of bytes (or perhaps even words) in
            size. If you want an array of bit-fields C doesn't support this directly
            but you can use C's boolean and shift operators to simulate this over an
            array of unsigned integer type.
            [color=blue]
            > second (not directly realated to the above) how i will assure that
            > structure is alligned to the machine lenght ? what i have to do in the
            > structure that make me sure that it is alligned to the machine word
            > lenght.[/color]

            You could allocate the structure using malloc() which on success
            returns a pointer to memory guaranteed to be appropriately aligned for any
            object type in C.
            [color=blue]
            >
            > I supposse that Strcuture padding is done to get the exectution of
            > Arthematic execution fast.... m i correct as it is alligned to the
            > machine length.[/color]

            The defined structure is at least aligned so that it can be accessed
            correctly. It may or may not be aligned more strictly than that e.g. to
            some "machine length".

            Lawrence

            Comment

            • Mohan

              #7
              Re: structure



              ranjeet.gupta@g mail.com wrote:[color=blue]
              > Dear All !!
              >
              > Before i qoute my querry, I will like to qoute my analysis and my
              > Knowledge
              >
              > Struct a {
              > int raw;
              > char data;
              > };
              >
              > the size of above struct is 8 byte ... correct (I am talking of
              > 32 bit procesor)
              >
              > Struct a {
              > int raw;
              > char data_1;
              > char data_2;
              > };
              >
              > again the size of above structure is 8 bytes.
              >
              > Means the structure of padding is going on... right...
              >
              > Now suppose I have to specefically allocate the 4 bits,
              > then What i will do is
              >
              > strcut a {
              >
              > cahr a:4;
              > }[/color]
              bitfield are appropriate for *int*s
              [color=blue]
              >
              > now ideally speaking i should get the size of the above struct as
              > 4 bits, But infact i will get the 1 byte, Again i run in the situation
              > that i dont want to allocate the extra 4 bits (which by default
              > structure is doing padding),[/color]

              You can get size of a structure (infact any data type or variable)
              atleast in multiples of byte.

              [color=blue]
              >
              > now my question comes.
              >
              > First
              > how I am going to allocate the 4 bits only,[/color]
              I think, allocating a memory of 4bits is not possible. However you can
              specify __attribute__(( __packed__)) along with structure definition to
              pack the allocation to byte aligned.
              [color=blue]
              >
              > second (not directly realated to the above)
              > how i will assure that structure is alligned to the machine lenght ?
              > what i have to do in the structure that make me sure that it is
              > alligned to the machine word lenght.[/color]

              __attribute__ ((__aligned__)) might help you.

              --
              Mohan
              [color=blue]
              >
              > I supposse that Strcuture padding is done to get the exectution of
              > Arthematic execution fast.... m i correct as it is alligned
              > to the machine length.
              >
              > Please let me know about the above querry
              >
              > Thanks In Advance
              > Ranjeet[/color]

              Comment

              • Richard Bos

                #8
                Re: structure

                ranjeet.gupta@g mail.com wrote:
                [color=blue]
                > Struct a {
                > int raw;
                > char data;
                > };
                >
                > the size of above struct is 8 byte ... correct (I am talking of
                > 32 bit procesor)[/color]

                Incorrect - or more precisely, you do not know this. There may or may
                not be padding; the size of int may or may not correspond to any of the
                measurements that may lead one to speak of a "32 bit processor"; char
                may be any number of bits.
                For example, some embedded devices could reasonably be called 32-bits
                processors, but on common C implementations for them, both char _and_
                int are 32 bits, and the above struct would probably be 2 bytes large,
                of 32 bits each.

                BTW, C is case-sensitive: that keyword is spelled struct, not Struct.
                [color=blue]
                > Struct a {
                > int raw;
                > char data_1;
                > char data_2;
                > };
                >
                > again the size of above structure is 8 bytes.[/color]

                See above: possibly, but you do not know this.
                [color=blue]
                > Means the structure of padding is going on... right...[/color]

                _If_ your assumptions about their sizes are correct, _then_ there is
                padding in those structs, yes.
                [color=blue]
                > Now suppose I have to specefically allocate the 4 bits,
                > then What i will do is
                >
                > strcut a {
                >
                > cahr a:4;
                > }
                >
                > now ideally speaking i should get the size of the above struct as
                > 4 bits,[/color]

                You can never get any stand-alone C object of 4 bits. All objects are
                made of a whole number of bytes, and all bytes are at least 8 bits
                large, possibly more.
                [color=blue]
                > But infact i will get the 1 byte, Again i run in the situation
                > that i dont want to allocate the extra 4 bits (which by default
                > structure is doing padding),[/color]

                No, it isn't; you've allocated a struct with a single char, and defined
                a bit field of four bits in that char.
                [color=blue]
                > First
                > how I am going to allocate the 4 bits only,[/color]

                Not. It isn't possible in C. It probably isn't possible in any language,
                in a meaningful way.
                [color=blue]
                > second (not directly realated to the above)
                > how i will assure that structure is alligned to the machine lenght ?[/color]

                That is not a meaningful question. First, define "machine length". Then,
                explain why you would need to align to it.

                All memory you get from malloc(), btw, is aligned to the requirements of
                any C type. How this alignment is related to a machine length, well...

                Richard

                Comment

                • Lawrence Kirby

                  #9
                  Re: structure

                  On Mon, 30 May 2005 04:13:29 -0700, Mohan wrote:

                  ....
                  [color=blue][color=green]
                  >> second (not directly realated to the above)
                  >> how i will assure that structure is alligned to the machine lenght ?
                  >> what i have to do in the structure that make me sure that it is
                  >> alligned to the machine word lenght.[/color]
                  >
                  > __attribute__ ((__aligned__)) might help you.[/color]

                  However no such construct exists in the C language. Some compilers might
                  support it as an extension but it is best to avoid compiler specific
                  constructs if you can.

                  Lawrence

                  Comment

                  • Chris Torek

                    #10
                    Re: structure

                    In article <1117451382.296 811.185920@z14g 2000cwz.googleg roups.com>
                    Mukul Pandey <mukul.pandey@g mail.com> quoted no context, and there
                    was no previous article on my news server, so I have no idea what
                    might have been in article
                    <1117449655.035 498.195390@o13g 2000cwo.googleg roups.com>, except that
                    obviously it must have something to do with structure members:
                    [color=blue]
                    >1. by saying,
                    >
                    > char a:4;
                    >
                    >you are not just allocating 4 bits, instead, you are requesting
                    >for a bit-wise allocation. Suppose, you know that you have three
                    >attributes, with known ranges, you could say,
                    >
                    > struct bitwise_alloc
                    > {
                    > char a:4; (range 0 to 15)
                    > char b:2; (range 0 to 3)
                    > char c:1; (range 0 to 1)
                    > char d:1;
                    > };
                    >
                    >The size would still be 1 byte (if padding is not there). No allocation
                    >below 1 byte is possible (for an object/variable).[/color]

                    Portable C code can only use "int", "signed int", and "unsigned
                    int" as bitfield base types. The above often works anyway though,
                    and is correct as far as it goes. (Note also that in C a "byte"
                    is a CHAR_BIT-bits-long entity, even if CHAR_BIT is more than 8.
                    This potentially differs from the 8-bit "octet" that most people
                    mean when they say "byte".)

                    There is a large problem with using plain "char" as the bitfield
                    type, however: plain char may be, and often is, signed. The
                    "a" member of the struct above would then have a guaranteed
                    range of -7 to +7 (and a typical range of -8 to +7), rather than
                    0 to 15. Instead of using plain (possibly-signed) char, if you
                    need the range to go from 0 to 15, use "unsigned char". (It
                    may still not even compile, if the compiler does not support
                    "char" types for bitfield members.)
                    [color=blue]
                    >2. you could do this using the compiler directive (#pragma pack),
                    >but then it might not be a generic implementation.[/color]

                    Indeed. Not only is "#pragma pack" nonstandard, but even those
                    compilers that do implement it, tend to implement it differently
                    -- for instance, GCC prefers the __attribute__ spelling. :-)

                    It is worth noting, however, that no matter whether you use "char"
                    bitfields, or "#pragma", or "__attribute__" , you have thrown
                    portability entirely out the window. The only difference is
                    how far out the window it has gone (and whether it has rolled
                    across the field and into the stream and run out with the river
                    to the sea :-) ).

                    In the C code I have seen, people who do this are usually attempting
                    to match an internal data structure to some external data format
                    (a file format, or network-data-stream, or hardware register, or
                    similar). Usually it *can* be done. Often, however, it is a
                    mistake even to try. C is not Ada; C has no representation clauses.
                    Instead of trying to trick the compiler into matching up an
                    internal representation with an external one, the C programmer is
                    often better off writing several small functions whose entire
                    purpose is to *translate* between the internal format and the
                    external one.

                    For instance, suppose that, instead of the "bitwise_al loc"
                    structure above, the goal is to read four values from a stdio
                    "FILE" object, whose ranges are 0..15, 0..3, 0..1, and 0..1,
                    and which are stored in an 8-bit octet with the "d" value in
                    bit zero, the "c" value in bit 7, the "b" value in bits 5
                    and 6 in backwards bit order (i.e., bit 5 is the high bit and
                    bit 6 is the low bit), and the "a" value in bits 4, 3, 2, and
                    1 in forwards order. Suppose also that we have:

                    struct value {
                    unsigned char val_a; /* range 0..15 */
                    unsigned char val_b; /* range 0..3 */
                    unsigned char val_c; /* range 0..1 */
                    unsigned char val_d; /* range 0..1 */
                    };

                    as the internal representation. Then we just need the following
                    function to read the external format:

                    int get_value(FILE *stream, struct value *result) {
                    int c;

                    if ((c = getc(stream)) == EOF)
                    return ERROR;
                    /*
                    * Value A is stored in bits 4..1: shift to 3..0 and mask.
                    */
                    result->val_a = (c >> 1) & 0xf; /* bits 4..1 */
                    /*
                    * Value B is stored in bits 5 (0x20) and 6 (0x40) but
                    * in backwards bit order. Move bit 6 to bit 0 (shift 6) and
                    * bit 5 to bit 1 (shift 4).
                    */
                    result->val_b = ((c & 0x20) >> 4) | ((c & 0x40) >> 6);
                    /*
                    * Value C is bit 7 (0x80), and value D is bit 0 (0x1).
                    */
                    result->val_c = (c & 0x80) >> 7;
                    result->val_d = c & 0x01;
                    return OK;
                    }

                    The function required to write the external format should now be
                    obvious (and is left as an exercise :-) ).
                    --
                    In-Real-Life: Chris Torek, Wind River Systems
                    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                    email: forget about it http://web.torek.net/torek/index.html
                    Reading email is like searching for food in the garbage, thanks to spammers.

                    Comment

                    • Lawrence Kirby

                      #11
                      Re: structure

                      On Mon, 30 May 2005 04:09:42 -0700, Mukul Pandey wrote:
                      [color=blue]
                      > 1. by saying,
                      >
                      > char a:4;[/color]

                      The valid types C supports for bit-fields are int, signed int, unsigned
                      int and in C99 _Bool. char isn't one of these, although an implementation
                      is allowed to support it as an extension.
                      [color=blue]
                      > you are not just allocating 4 bits, instead, you are requesting for a
                      > bit-wise allocation. Suppose, you know that you have three attributes,
                      > with known ranges, you could say,
                      >
                      > struct bitwise_alloc
                      > {
                      > char a:4; (range 0 to 15)
                      > char b:2; (range 0 to 3)
                      > char c:1; (range 0 to 1)
                      > char d:1;
                      > };[/color]

                      Plain char can be signed or unsigned so it isn't clear that the ranges
                      would be as you say.

                      Lawrence

                      Comment

                      Working...