<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1187.34">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 12.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 12.0px; font: 12.0px Helvetica; color: #011892}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #000000; min-height: 14.0px}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #000000}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}
p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}
p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}
p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #929292}
span.s1 {color: #bb2ca2}
span.s2 {color: #008400}
span.s3 {color: #000000}
span.s4 {color: #3d1d81}
span.s5 {color: #4f8187}
span.s6 {color: #d12f1b}
span.s7 {color: #78492a}
span.Apple-tab-span {white-space:pre}
</style>
</head>
<body>
<p class="p1">On 2012-10-08 23:24:42 +0000, James Cameron said:</p>
<p class="p2"><br></p>
<p class="p3">48 bytes is wrong.<span class="Apple-converted-space">  </span>32 bytes is right.<span class="Apple-converted-space">  </span>Your compiler might be</p>
<p class="p3">aligning the structure members for performance.<span class="Apple-converted-space">  </span>Give a suitable</p>
<p class="p3">alignment directive to your compiler.<span class="Apple-converted-space">  </span>We use __attribute__((packed))</p>
<p class="p3">in the server code.<span class="Apple-converted-space">  </span>Check the offset to each member using whatever</p>
<p class="p3">language facilities are available.<span class="Apple-converted-space">  </span>In C we can use sizeof() and</p>
<p class="p3">offsetof().</p>
<p class="p4"><br></p>
<p class="p5">So here's the debug using Jame's excellent advise.</p>
<p class="p4"><br></p>
<p class="p6"><span class="s1">typedef</span> <span class="s1">struct</span> _youServerPacket</p>
<p class="p6">{</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">char</span><span class="Apple-converted-space">    </span>type;<span class="Apple-tab-span">    </span><span class="Apple-tab-span">      </span><span class="Apple-tab-span">      </span><span class="Apple-tab-span">      </span> <span class="s2">/* SP_YOU */</span></p>
<p class="p7"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s1">char</span><span class="s3"><span class="Apple-converted-space">    </span>pnum;<span class="Apple-tab-span">     </span><span class="Apple-tab-span">      </span><span class="Apple-tab-span">      </span><span class="Apple-tab-span">      </span> </span>/* Guy needs to know this... */</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">char</span><span class="Apple-converted-space">    </span>hostile;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">char</span><span class="Apple-converted-space">    </span>swar;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">char</span><span class="Apple-converted-space">    </span>armies;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">char</span><span class="Apple-converted-space">    </span>tractor;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">char</span><span class="Apple-converted-space">    </span>pad2;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">char</span><span class="Apple-converted-space">    </span>pad3;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">unsigned</span> flags;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">long</span><span class="Apple-converted-space">    </span>damage;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">long</span><span class="Apple-converted-space">    </span>shield;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">long</span><span class="Apple-converted-space">    </span>fuel;</p>
<p class="p8"><span class="s3"><span class="Apple-converted-space">    </span></span>unsigned<span class="s3"> </span>short<span class="s3"> etemp;</span></p>
<p class="p8"><span class="s3"><span class="Apple-converted-space">    </span></span>unsigned<span class="s3"> </span>short<span class="s3"> wtemp;</span></p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">unsigned</span> <span class="s1">short</span> whydead;</p>
<p class="p6"><span class="Apple-converted-space">    </span><span class="s1">unsigned</span> <span class="s1">short</span> whodead;</p>
<p class="p6">} <span class="s1">__attribute__</span> ((packed)) youServerPacketStruct;</p>
<p class="p4"><br></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span>size<span class="s3"> = </span><span class="s1">sizeof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">);</span></p>
<p class="p10"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span>@"ServerPacketYou size=%d"<span class="s3">, </span><span class="s5">size</span><span class="s3">);</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"type %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>type<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"pnum %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>pnum<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"hostile %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>hostile<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"swar %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>swar<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"armies %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>armies<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"tractor %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>tractor<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"pad2 %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>pad2<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"pad3 %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>pad3<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">char</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"flags %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>flags<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">unsigned</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"damge %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>damage<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">long</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"shield %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>shield<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">long</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"fuel %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>fuel<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">long</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"etemp %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>etemp<span class="s3">) , </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">unsigned</span><span class="s3"> </span><span class="s1">short</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"wtemp %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>wtemp<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">unsigned</span><span class="s3"> </span><span class="s1">short</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"whydead %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>whydead<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">unsigned</span><span class="s3"> </span><span class="s1">short</span><span class="s3">));</span></p>
<p class="p9"><span class="s3"><span class="Apple-converted-space">    </span></span><span class="s4">NSLog</span><span class="s3">(</span><span class="s6">@"whodead %lu:%ld\n"</span><span class="s3">, </span><span class="s7">offsetof</span><span class="s3">(</span>youServerPacketStruct<span class="s3">, </span>whodead<span class="s3">), </span><span class="s1">sizeof</span><span class="s3">(</span><span class="s1">unsigned</span><span class="s3"> </span><span class="s1">short</span><span class="s3">));</span></p>
<p class="p4"><br></p>
<p class="p6"><b>2012-10-08 22:48:06.564 Packet[10189:303] ServerPacketYou size=44</b></p>
<p class="p6"><b>2012-10-08 22:45:42.254 Packet[10152:303] type 0:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.255 Packet[10152:303] pnum 1:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.255 Packet[10152:303] hostile 2:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.255 Packet[10152:303] swar 3:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.256 Packet[10152:303] armies 4:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.256 Packet[10152:303] tractor 5:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.256 Packet[10152:303] pad2 6:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.256 Packet[10152:303] pad3 7:1</b></p>
<p class="p6"><b>2012-10-08 22:45:42.256 Packet[10152:303] flags 8:4</b></p>
<p class="p6"><b>2012-10-08 22:45:42.257 Packet[10152:303] damge 12:8</b></p>
<p class="p6"><b>2012-10-08 22:45:42.257 Packet[10152:303] shield 20:8</b></p>
<p class="p6"><b>2012-10-08 22:45:42.257 Packet[10152:303] fuel 28:8</b></p>
<p class="p6"><b>2012-10-08 22:45:42.257 Packet[10152:303] etemp 36:2</b></p>
<p class="p6"><b>2012-10-08 22:45:42.257 Packet[10152:303] wtemp 38:2</b></p>
<p class="p6"><b>2012-10-08 22:45:42.258 Packet[10152:303] whydead 40:2</b></p>
<p class="p6"><b>2012-10-08 22:45:42.258 Packet[10152:303] whodead 42:2</b></p>
<p class="p11"><br></p>
<p class="p12">NOTE that sizeof(long) = 8, so I wiped up this program.<span class="Apple-converted-space"> </span></p>
<p class="p11"><br></p>
<p class="p12">Compiling with -m32 I get sizeof(long) = 4</p>
<p class="p11"><br></p>
<p class="p12">#include <stdio.h></p>
<p class="p11"><br></p>
<p class="p12">main()</p>
<p class="p12">{</p>
<p class="p12"><span class="Apple-converted-space">    </span>printf("char %ld\n", sizeof(char));</p>
<p class="p12"><span class="Apple-converted-space">    </span>printf("unsigned %ld\n", sizeof(unsigned));</p>
<p class="p12"><span class="Apple-converted-space">    </span>printf("long %ld\n", sizeof(long));</p>
<p class="p12"><span class="Apple-converted-space">    </span>printf("unsigned short %ld\n", sizeof(unsigned short));</p>
<p class="p12">}</p>
<p class="p11"><br></p>
<p class="p12">$ gcc size.c ; ./a.out</p>
<p class="p12">char 1</p>
<p class="p12">unsigned 4</p>
<p class="p12">long 8</p>
<p class="p12">unsigned short 2</p>
<p class="p11"><br></p>
<p class="p12">$ gcc -m32 size.c ; ./a.out</p>
<p class="p12">char 1</p>
<p class="p12">unsigned 4</p>
<p class="p12">long 4</p>
<p class="p12">unsigned short 2</p>
<p class="p11"><br></p>
<p class="p12">Recommendations (sorry tired and lazy, so throwing it to the mailing list for answers)?</p>
<p class="p11"><br></p>
<p class="p12">Guess I can look up the -std=c99 stuff?</p>
<p class="p13">--<span class="Apple-converted-space"> </span></p>
<p class="p13">Bob Tanner <basic@us.netrek.org> <span class="Apple-converted-space">         </span></p>
<p class="p13">Key fingerprint = 9906 320A 8BB6 64AD 96A7<span class="Apple-converted-space">  </span>7785 CBFB 10BF 568B F98C</p>
</body>
</html>