From: Robert Jaycocks Subject: Re: Sizeof structure different in DJGPP? To: MWODRICH AT eleceng DOT uct DOT ac DOT za (WODRICH M M WDRMAR002) Date: Fri, 24 Mar 95 11:54:04 GMT Cc: djgpp AT sun DOT soe DOT clarkson DOT edu > > Hello > > I seem to come up with a new question every day almost, but here goes! > I have a structure as follows in my program : > > struct something > { > char ch; > unsigned short int number; > }; > > In Turbo-C the sizeof this struct is, as expected, 3 bytes. In DJGPP > it is 4 bytes ???! The sizeof (char) and sizeof (unsigned short int) > is 1 & 2 respectively in both compilers... Does DJGPP Word-align data, > promoting the 3-byte strucure to 4 bytes? The reason I am asking is > that I want to store many (60,000) such structs in an array, and > wasting 60Kb+ in DJGPP seems crazy, even though memory is no problem. > I want to avoid paging on small systems unless absolutely > neccessary (sp?), so the extra 1 byte is irritating me... > > Thanks for an informative mailing list! > Mark.-- > > Mark Wodrich Email : mwodrich AT eleceng DOT uct DOT ac DOT za > 13 Letchworth Road > Pinelands " Excuse me, but I think your karma has > 7405 just run over my dogma ..." > You can use the variable attribute packed, as in : struct something { char ch _attribute_ ((packed)); u_short number _attribute_ ((packed)); }; Doing a sizeof(struct something) should now return 3. Refer to the GNU GCC Info page on attribute variables. Hope this helps Robert.