comparing sizes of two structures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    comparing sizes of two structures

    Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
    TYPE5, of data.

    Consider the structures:

    struct struct_one
    {
    TYPE1 data1;
    TYPE2 data2;
    TYPE3 data3;
    TYPE4 data4;
    TYPE5 data5;
    } var_one;

    struct struct_two
    {
    TYPE2 data1;
    TYPE5 data2;
    TYPE1 data3;
    TYPE3 data4;
    TYPE4 data5;
    } var_two;

    How do the sizes of var_one and var_two compare - will they be the
    same or different ?

  • Ian Collins

    #2
    Re: comparing sizes of two structures

    subramanian100i n@yahoo.com, India wrote:
    Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
    TYPE5, of data.
    >
    Consider the structures:
    >
    struct struct_one
    {
    TYPE1 data1;
    TYPE2 data2;
    TYPE3 data3;
    TYPE4 data4;
    TYPE5 data5;
    } var_one;
    >
    struct struct_two
    {
    TYPE2 data1;
    TYPE5 data2;
    TYPE1 data3;
    TYPE3 data4;
    TYPE4 data5;
    } var_two;
    >
    How do the sizes of var_one and var_two compare - will they be the
    same or different ?
    >
    Either, it depends on the types and the implementation.

    --
    Ian Collins.

    Comment

    • santosh

      #3
      Re: comparing sizes of two structures

      subramanian100i n@yahoo.com, India wrote:
      Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
      TYPE5, of data.
      >
      Consider the structures:
      >
      struct struct_one
      {
      TYPE1 data1;
      TYPE2 data2;
      TYPE3 data3;
      TYPE4 data4;
      TYPE5 data5;
      } var_one;
      >
      struct struct_two
      {
      TYPE2 data1;
      TYPE5 data2;
      TYPE1 data3;
      TYPE3 data4;
      TYPE4 data5;
      } var_two;
      >
      How do the sizes of var_one and var_two compare - will they be the
      same or different ?
      It depends on the compiler and the characteristics , (mainly alignment
      requirements), of your types.

      Comment

      • jaysome

        #4
        Re: comparing sizes of two structures

        On 7 Mar 2007 00:16:50 -0800, "subramanian100 in@yahoo.com, India"
        <subramanian100 in@yahoo.comwro te:
        >Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
        >TYPE5, of data.
        >
        >Consider the structures:
        >
        >struct struct_one
        {
        TYPE1 data1;
        TYPE2 data2;
        TYPE3 data3;
        TYPE4 data4;
        TYPE5 data5;
        >} var_one;
        >
        >struct struct_two
        {
        TYPE2 data1;
        TYPE5 data2;
        TYPE1 data3;
        TYPE3 data4;
        TYPE4 data5;
        >} var_two;
        >
        >How do the sizes of var_one and var_two compare - will they be the
        >same or different ?
        They could be the same or they could be different. It depends on the
        exact types of TYPE* and on the compiler. Compilers can add padding in
        order to align fields on their natural boundaries, and there is no
        guarantee that one compiler will pack fields the same as another
        compiler, especially so when the compilers target different platforms.

        Try compiling and running the following:

        #include <stddef.h>
        #include <stdio.h>

        typedef char TYPE1;
        typedef double TYPE2;
        typedef int TYPE3;
        typedef short TYPE4;
        typedef long TYPE5;

        struct s1
        {
        TYPE1 d1;
        TYPE2 d2;
        TYPE3 d3;
        TYPE4 d4;
        TYPE5 d5;
        } var_one;

        struct s2
        {
        TYPE2 d1;
        TYPE5 d2;
        TYPE1 d3;
        TYPE3 d4;
        TYPE4 d5;
        } var_two;

        int main(void)
        {
        printf("s1 offsets:\n");
        printf("offseto f(d1) = %d\n", (int)offsetof(s truct s1, d1));
        printf("offseto f(d2) = %d\n", (int)offsetof(s truct s1, d2));
        printf("offseto f(d3) = %d\n", (int)offsetof(s truct s1, d3));
        printf("offseto f(d4) = %d\n", (int)offsetof(s truct s1, d4));
        printf("offseto f(d5) = %d\n", (int)offsetof(s truct s1, d5));

        printf("\n");
        printf("s2 offsets:\n");
        printf("offseto f(d1) = %d\n", (int)offsetof(s truct s2, d1));
        printf("offseto f(d2) = %d\n", (int)offsetof(s truct s2, d2));
        printf("offseto f(d3) = %d\n", (int)offsetof(s truct s2, d3));
        printf("offseto f(d4) = %d\n", (int)offsetof(s truct s2, d4));
        printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));

        return 0;
        }

        My conforming C implementation outputs:

        s1 offsets:
        offsetof(d1) = 0
        offsetof(d2) = 8
        offsetof(d3) = 16
        offsetof(d4) = 20
        offsetof(d5) = 24

        s2 offsets:
        offsetof(d1) = 0
        offsetof(d2) = 8
        offsetof(d3) = 12
        offsetof(d4) = 16
        offsetof(d5) = 20
        Press any key to continue

        Regards
        --
        jay

        Comment

        • Beej Jorgensen

          #5
          Re: comparing sizes of two structures

          subramanian100i n@yahoo.com, India <subramanian100 in@yahoo.comwro te:
          >How do the sizes of var_one and var_two compare - will they be the
          >same or different ?
          Like others have said, it depends, but I'd answer in general "probably
          different".

          In the last big project I worked on, we'd allocate tons of structures at
          run time, and the platform was relatively limited in memory. As part of
          our optimizations, we went back and rearranged the fields in the structs
          by hand so the compiler would put less padding in.

          We realized significant memory savings.

          -Beej

          Comment

          • Chris Dollin

            #6
            Re: comparing sizes of two structures

            subramanian100i n@yahoo.com, India wrote:
            Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
            TYPE5, of data.
            >
            Consider the structures:
            >
            struct struct_one
            {
            TYPE1 data1;
            TYPE2 data2;
            TYPE3 data3;
            TYPE4 data4;
            TYPE5 data5;
            } var_one;
            >
            struct struct_two
            {
            TYPE2 data1;
            TYPE5 data2;
            TYPE1 data3;
            TYPE3 data4;
            TYPE4 data5;
            } var_two;
            >
            How do the sizes of var_one and var_two compare - will they be the
            same or different ?
            Yes, one or the other.

            Why do you care?

            --
            Chris "electric hedgehog" Dollin
            "Possibly you're not recalling some of his previous plans." Zoe, /Firefly/

            Comment

            • santosh

              #7
              Re: comparing sizes of two structures

              jaysome wrote:
              On 7 Mar 2007 00:16:50 -0800, "subramanian100 in@yahoo.com, India"
              <subramanian100 in@yahoo.comwro te:
              >
              Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
              TYPE5, of data.

              Consider the structures:

              struct struct_one
              {
              TYPE1 data1;
              TYPE2 data2;
              TYPE3 data3;
              TYPE4 data4;
              TYPE5 data5;
              } var_one;

              struct struct_two
              {
              TYPE2 data1;
              TYPE5 data2;
              TYPE1 data3;
              TYPE3 data4;
              TYPE4 data5;
              } var_two;

              How do the sizes of var_one and var_two compare - will they be the
              same or different ?
              [ ... ]
              Try compiling and running the following:
              >
              #include <stddef.h>
              #include <stdio.h>
              >
              typedef char TYPE1;
              typedef double TYPE2;
              typedef int TYPE3;
              typedef short TYPE4;
              typedef long TYPE5;
              >
              struct s1
              {
              TYPE1 d1;
              TYPE2 d2;
              TYPE3 d3;
              TYPE4 d4;
              TYPE5 d5;
              } var_one;
              >
              struct s2
              {
              TYPE2 d1;
              TYPE5 d2;
              TYPE1 d3;
              TYPE3 d4;
              TYPE4 d5;
              } var_two;
              >
              int main(void)
              {
              printf("s1 offsets:\n");
              printf("offseto f(d1) = %d\n", (int)offsetof(s truct s1, d1));
              printf("offseto f(d2) = %d\n", (int)offsetof(s truct s1, d2));
              printf("offseto f(d3) = %d\n", (int)offsetof(s truct s1, d3));
              printf("offseto f(d4) = %d\n", (int)offsetof(s truct s1, d4));
              printf("offseto f(d5) = %d\n", (int)offsetof(s truct s1, d5));
              >
              printf("\n");
              printf("s2 offsets:\n");
              printf("offseto f(d1) = %d\n", (int)offsetof(s truct s2, d1));
              printf("offseto f(d2) = %d\n", (int)offsetof(s truct s2, d2));
              printf("offseto f(d3) = %d\n", (int)offsetof(s truct s2, d3));
              printf("offseto f(d4) = %d\n", (int)offsetof(s truct s2, d4));
              printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));
              >
              return 0;
              }
              Why do cast the type size_t value yielded by offsetof into int?

              <snip>

              Comment

              • Chris Dollin

                #8
                Re: comparing sizes of two structures

                santosh wrote:
                jaysome wrote:
                (quelle snippage!)
                > printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));
                >>
                > return 0;
                >}
                >
                Why do cast the type size_t value yielded by offsetof into int?
                So they can print them using %d?

                --
                Chris "electric hedgehog" Dollin
                The shortcuts are all full of people using them.

                Comment

                • santosh

                  #9
                  Re: comparing sizes of two structures

                  Chris Dollin wrote:
                  santosh wrote:
                  >
                  jaysome wrote:
                  >
                  (quelle snippage!)
                  >
                  printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));
                  >
                  return 0;
                  }
                  Why do cast the type size_t value yielded by offsetof into int?
                  >
                  So they can print them using %d?
                  I know that. I would've myself used %lu, if %zu was not possible,
                  (since jaysome appears to use Microsoft's Visual C++, which doesn't
                  implement C99).

                  So it was a bit strange.

                  Comment

                  • Yevgen Muntyan

                    #10
                    Re: comparing sizes of two structures

                    santosh wrote:
                    Chris Dollin wrote:
                    >santosh wrote:
                    >>
                    >>jaysome wrote:
                    >(quelle snippage!)
                    >>
                    >>> printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));
                    >>>>
                    >>> return 0;
                    >>>}
                    >>Why do cast the type size_t value yielded by offsetof into int?
                    >So they can print them using %d?
                    >
                    I know that. I would've myself used %lu, if %zu was not possible,
                    So you'd cast to long instead of int.
                    (since jaysome appears to use Microsoft's Visual C++, which doesn't
                    implement C99).
                    Microsoft is one of funny vendors who have long smaller than size_t.

                    Yevgen

                    Comment

                    • Rahul

                      #11
                      Re: comparing sizes of two structures

                      hi,
                      you can handle this byte alignment issue by using
                      __attribute__(( __packed__)) with each structure element. then each one
                      of them will be allocated only minimum number of bytes needed.You can
                      save the memory requirements if u have huge structures.
                      thanx.
                      rk.
                      subramanian100i n@yahoo.com, India wrote:
                      Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
                      TYPE5, of data.
                      >
                      Consider the structures:
                      >
                      struct struct_one
                      {
                      TYPE1 data1;
                      TYPE2 data2;
                      TYPE3 data3;
                      TYPE4 data4;
                      TYPE5 data5;
                      } var_one;
                      >
                      struct struct_two
                      {
                      TYPE2 data1;
                      TYPE5 data2;
                      TYPE1 data3;
                      TYPE3 data4;
                      TYPE4 data5;
                      } var_two;
                      >
                      How do the sizes of var_one and var_two compare - will they be the
                      same or different ?

                      Comment

                      • R Pradeep Chandran

                        #12
                        Re: comparing sizes of two structures

                        On 7 Mar 2007 06:54:09 -0800, "Rahul" <rahul.kashyap9 8@gmail.com>
                        wrote:
                        >you can handle this byte alignment issue by using
                        >__attribute__( (__packed__)) with each structure element. then each one
                        >of them will be allocated only minimum number of bytes needed.You can
                        >save the memory requirements if u have huge structures.
                        This is an implementation specific feature and is not portable.

                        When you are offering implementation specific suggestions, it would be
                        a very good idea to mark it as such.

                        Have a nice day,
                        Pradeep
                        --
                        All opinions are mine and do not represent the views or
                        policies of my employer.
                        R Pradeep Chandran rpc AT pobox DOT com

                        Comment

                        • Dan Becker

                          #13
                          Re: comparing sizes of two structures


                          <subramanian100 in@yahoo.comwro te in message
                          news:1173255410 .509278.50330@c 51g2000cwc.goog legroups.com...
                          Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
                          TYPE5, of data.
                          >
                          Consider the structures:
                          >
                          struct struct_one
                          {
                          TYPE1 data1;
                          TYPE2 data2;
                          TYPE3 data3;
                          TYPE4 data4;
                          TYPE5 data5;
                          } var_one;
                          >
                          struct struct_two
                          {
                          TYPE2 data1;
                          TYPE5 data2;
                          TYPE1 data3;
                          TYPE3 data4;
                          TYPE4 data5;
                          } var_two;
                          >
                          How do the sizes of var_one and var_two compare - will they be the
                          same or different ?
                          >
                          might be different, or might be same.
                          The sizes could be different, depending on what TYPE[1-5] are, and what
                          aliignment constraints are imposed by the platform.

                          Dan


                          Comment

                          • Flash Gordon

                            #14
                            Re: comparing sizes of two structures

                            Yevgen Muntyan wrote, On 07/03/07 14:15:
                            santosh wrote:
                            >Chris Dollin wrote:
                            >>santosh wrote:
                            >>>
                            >>>jaysome wrote:
                            >>(quelle snippage!)
                            >>>
                            >>>> printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));
                            >>>>>
                            >>>> return 0;
                            >>>>}
                            >>>Why do cast the type size_t value yielded by offsetof into int?
                            >>So they can print them using %d?
                            >>
                            >I know that. I would've myself used %lu, if %zu was not possible,
                            >
                            So you'd cast to long instead of int.
                            No, read it again. Santosh would have to cast to unsigned long. Casting
                            size_t to an unsigned type makes far more sense than a signed type
                            (size_t being unsigned). unsigned long makes sense because it is the
                            largest unsigned type a C90 implementation is guaranteed to have.
                            >(since jaysome appears to use Microsoft's Visual C++, which doesn't
                            >implement C99).
                            >
                            Microsoft is one of funny vendors who have long smaller than size_t.
                            It does make a certain amount of sense on 64 bit platforms. Although I
                            would have gone for increasing the size of long to 64 bits myself.
                            --
                            Flash Gordon

                            Comment

                            • matevzb

                              #15
                              Re: comparing sizes of two structures

                              On Mar 7, 3:15 pm, Yevgen Muntyan <muntyan.remove t...@tamu.eduwr ote:
                              <snip>
                              (since jaysome appears to use Microsoft's Visual C++, which doesn't
                              implement C99).
                              >
                              Microsoft is one of funny vendors who have long smaller than size_t.
                              Um... not on my 32-bit (XP), they're both 4 bytes and size_t is a
                              typedef to unsigned int. On 64-bit it's a typedef to __int64, and
                              (IIRC) they decided to stick with 32-bit long.
                              --
                              WYCIWYG - what you C is what you get

                              Comment

                              Working...