Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!bu.edu!bucsf.bu.edu!jdubb From: jdubb@bucsf.bu.edu (jay dubb) Newsgroups: comp.unix.programmer Subject: why do structs have different sizes across machines? Message-ID: <77336@bu.edu.bu.edu> Date: 20 Mar 91 00:26:07 GMT Sender: news@bu.edu.bu.edu Distribution: usa Organization: Computer Science Department, Boston University, Boston, MA, USA Lines: 34 Originator: jdubb@bucsf.bu.edu I am posting this for a friend of mine who doesn't have access to USENET, so please respond directly to mlevin@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@jade.tufts.edu)