From: "XtReMe" Newsgroups: comp.os.msdos.djgpp Subject: Re: odd sizeof results Date: Sun, 28 Feb 1999 02:22:21 +0100 Message-ID: <920164868.21105.0.rover.d4ee0fa5@news.demon.nl> References: <7at84k$etf$1 AT news5 DOT svr DOT pol DOT co DOT uk> <36D23316 DOT 5506DAC8 AT a DOT crl DOT com> NNTP-Posting-Host: x-project.demon.nl X-NNTP-Posting-Host: x-project.demon.nl:212.238.15.165 X-Trace: news.demon.nl 920164868 rover:21105 NO-IDENT x-project.demon.nl:212.238.15.165 X-Complaints-To: abuse AT demon DOT net X-Newsreader: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Lines: 46 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Weiqi Gao heeft geschreven in bericht <36D23316 DOT 5506DAC8 AT a DOT crl DOT com>... >> "<---------------------------" wrote: >> >> I have a structure that contains an unsigned char (typedefed for >> convenience) and a pointer to the next structure in my list. >> Because list traversal has been giving some dodgy results I set >> about checking everything - until I found this... >> >> typedef unsigned char number >> >> struct number_t >> { >> number value; >> struct number_t *next; >> }; >> >> struct number_t test; >> >> sizeof(struct number_t) = 8 bytes??? >> >> yet >> >> sizeof(test.value) = 1 and >> sizeof(test.next) = 4. >> >> the last maths book I checked said that 1 + 4 was still five. >> Any help would be greatly appreciated. > >The three extra bytes in the struct is caused by alignment and is >normal. > Yes, but if you want those struct's to be as tiny as possible use the '__attribute__ ((packed))' attribute. like this : struct number_t { number value __attribute__ ((packed)); struct number_t *next __attribute__ ((packed)); }; But I still find it curious when you define these attributes, and the struct's still aren't packed. Has this to do with optimisations or something?