comparing sizes of two structures

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

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 ?
 
I

Ian Collins

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.
 
S

santosh

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.
 
J

jaysome

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
 
B

Beej Jorgensen

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
 
C

Chris Dollin

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?
 
S

santosh

[ ... ]
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>
 
S

santosh

Chris said:
(quelle snippage!)


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.
 
Y

Yevgen Muntyan

santosh said:
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
 
R

Rahul

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.
 
R

R Pradeep Chandran

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
 
D

Dan Becker

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
 
F

Flash Gordon

Yevgen Muntyan wrote, On 07/03/07 14:15:
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.
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.
 
M

matevzb

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.
 
Y

Yevgen Muntyan

Flash said:
Yevgen Muntyan wrote, On 07/03/07 14:15:

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.

Wording sloppiness, "long" meant "unsigned long", I just wanted
to say he'd cast anyway (and for practical purposes int is as good
if you take structure offset).
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.

Yes, it's win64. I also agree it's no good.

Yevgen
 
Y

Yevgen Muntyan

matevzb said:
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.

So long is 4 bytes, and size_t is 8 bytes, i.e. long is smaller than
size_t (where "smaller" means sizeof...).

Yevgen
 
F

Flash Gordon

Yevgen Muntyan wrote, On 07/03/07 17:51:
Wording sloppiness, "long" meant "unsigned long", I just wanted
to say he'd cast anyway (and for practical purposes int is as good
if you take structure offset).

It makes a big difference to the correctness of the code.
Yes, it's win64. I also agree it's no good.

Read what I said again. It does make a certain amount of sense, it is
just not what I would have done. MS wanted to avoid breaking other
peoples code more than they needed to (possibly their own as well) and
made the choice on that basis.
 
K

Keith Thompson

Rahul said:
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.
[...]

Please don't top-post. See:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

Please don't use silly abbreviations like "u". This isn't a chat
room. Nobody expects perfect English, but please do take the effort
to spell out words.

"__attribute__((__packed__))" is a compiler-specific extension.
 
Y

ymuntyan

Yevgen Muntyan wrote, On 07/03/07 17:51:





It makes a big difference to the correctness of the code.

It's easier to write %d and (int) in ten line programs whose whole
purpose is to print structure member offsets. Note that the
program *was* correct (there doesn't exist a computer where
2*sizeof(double)+2*sizeof(long)+padding > INTMAX).
Besides, ironically, cast to unsigned long is almost as bad
as cast to int. If implementation was sufficiently weird (which
it isn't), both could break. unsigned long is as correct
as int: both can break (in weird theory); both ranges are smaller than
size_t range on some *existing* implementations; both work on all
existing implementations. Once something goes out the int range,
it will break on win64 if you use long ;)
Read what I said again. It does make a certain amount of sense, it is
just not what I would have done. MS wanted to avoid breaking other
peoples code more than they needed to (possibly their own as well) and
made the choice on that basis.

Sorry, I read "not what I would have done" as "it would be better in
my opinion not to make that". It is not quite "no good", so I could
not agree it's no good since you didn't say it's no good. I apologize
for making it look like I think you think MS did no good thing.

Yevgen
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top