why do structs have different sizes across machines?

jay dubb jdubb at bucsf.bu.edu
Wed Mar 20 10:26:07 AEST 1991


   I am posting this for a friend of mine who doesn't have access
to USENET, so please respond directly to mlevin at jade.tufts.edu.

   Can anyone explain to me why the following short program give the
size of the structure as 38 on a Sun 3, and 40 on an Encore Multimax:

main()
{
  struct tt
    {
      enum {P, PP} a;
      char b[30];
      int c;
    };
  printf("%d\n",sizeof(struct tt));
}

   I notice that making the size of b[] 32, makes the structure be 40
bytes large on both machines. I imagine thiis is due to the way the
machines align fields in structures. I am worried since I am trying to
send structures across sockets between en encore and a sun machine.
So, can someone enlighten me about the following things:
1) why exactly are the sizes different?
2) what reprecussions does this have on sending the structures across
   sockets between machines which pack them differently and trying to
   interpret the fields on each end?
3) and most importantly, how can I avoid this problem? are there some
   rules (for example, I know that all structures have to be a
   multiple of 4 bytes large, and I know about compensating for byte
   order differences, etc.) to be followed in making up structures
   that will ensure that they are the same on any machine?
Thanks in advance.
					Mike Levin (mlevin at jade.tufts.edu)



More information about the Comp.unix.programmer mailing list