Velocity Reviews > comparing sizes of two structures

# comparing sizes of two structures

subramanian100in@yahoo.com, India
Guest
Posts: n/a

 03-07-2007
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
Guest
Posts: n/a

 03-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed), 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.

santosh
Guest
Posts: n/a

 03-07-2007
(E-Mail Removed), 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

jaysome
Guest
Posts: n/a

 03-07-2007
On 7 Mar 2007 00:16:50 -0800, "(E-Mail Removed), India"
<(E-Mail Removed)> 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 ?

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("offsetof(d1) = %d\n", (int)offsetof(struct s1, d1));
printf("offsetof(d2) = %d\n", (int)offsetof(struct s1, d2));
printf("offsetof(d3) = %d\n", (int)offsetof(struct s1, d3));
printf("offsetof(d4) = %d\n", (int)offsetof(struct s1, d4));
printf("offsetof(d5) = %d\n", (int)offsetof(struct s1, d5));

printf("\n");
printf("s2 offsets:\n");
printf("offsetof(d1) = %d\n", (int)offsetof(struct s2, d1));
printf("offsetof(d2) = %d\n", (int)offsetof(struct s2, d2));
printf("offsetof(d3) = %d\n", (int)offsetof(struct s2, d3));
printf("offsetof(d4) = %d\n", (int)offsetof(struct s2, d4));
printf("offsetof(d5) = %d\n", (int)offsetof(struct 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

Beej Jorgensen
Guest
Posts: n/a

 03-07-2007
(E-Mail Removed), India <(E-Mail Removed)> wrote:
>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

Chris Dollin
Guest
Posts: n/a

 03-07-2007
(E-Mail Removed), 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/

santosh
Guest
Posts: n/a

 03-07-2007
jaysome wrote:
> On 7 Mar 2007 00:16:50 -0800, "(E-Mail Removed), India"
> <(E-Mail Removed)> 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 ?

[ ... ]

> 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("offsetof(d1) = %d\n", (int)offsetof(struct s1, d1));
> printf("offsetof(d2) = %d\n", (int)offsetof(struct s1, d2));
> printf("offsetof(d3) = %d\n", (int)offsetof(struct s1, d3));
> printf("offsetof(d4) = %d\n", (int)offsetof(struct s1, d4));
> printf("offsetof(d5) = %d\n", (int)offsetof(struct s1, d5));
>
> printf("\n");
> printf("s2 offsets:\n");
> printf("offsetof(d1) = %d\n", (int)offsetof(struct s2, d1));
> printf("offsetof(d2) = %d\n", (int)offsetof(struct s2, d2));
> printf("offsetof(d3) = %d\n", (int)offsetof(struct s2, d3));
> printf("offsetof(d4) = %d\n", (int)offsetof(struct s2, d4));
> printf("offsetof(d5) = %d\n", (int)offsetof(struct s2, d5));
>
> return 0;
> }

Why do cast the type size_t value yielded by offsetof into int?

<snip>

Chris Dollin
Guest
Posts: n/a

 03-07-2007
santosh wrote:

> jaysome wrote:

(quelle snippage!)

>> printf("offsetof(d5) = %d\n", (int)offsetof(struct 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.

santosh
Guest
Posts: n/a

 03-07-2007
Chris Dollin wrote:
> santosh wrote:
>
> > jaysome wrote:

>
> (quelle snippage!)
>
> >> printf("offsetof(d5) = %d\n", (int)offsetof(struct 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.

Yevgen Muntyan
Guest
Posts: n/a

 03-07-2007
santosh wrote:
> Chris Dollin wrote:
>> santosh wrote:
>>
>>> jaysome wrote:

>> (quelle snippage!)
>>
>>>> printf("offsetof(d5) = %d\n", (int)offsetof(struct 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post why? Computer Support 0 03-21-2010 11:32 AM why? Computer Support 0 03-21-2010 11:31 AM junky_fellow@yahoo.co.in C Programming 18 09-16-2007 01:49 AM Marful Computer Support 11 03-08-2006 07:13 PM Alfonso Morra C Programming 11 09-24-2005 07:42 PM