From: roland DOT nilsson AT communique DOT se (Roland Nilsson) Newsgroups: comp.os.msdos.djgpp Subject: Re: __attribute__((packed)) on types Date: 5 Jan 1997 17:47:59 GMT Organization: -+- Private -+- Lines: 40 Message-ID: <5aopgf$31h@news.luth.se> References: NNTP-Posting-Host: x6.communique.se Mime-Version: 1.0 Content-Type: Text/Plain; charset=US-ASCII To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp In article , eliz AT is DOT elta DOT co DOT il says... > > >On 4 Jan 1997, Roland Nilsson wrote: > >> struct foo >> { >> char a; >> long b; >> } __attribute__((packed)); >> [...] > >This doesn't work for C++ programs, AFAIK. In C++, you need to declare >each struct field with __attribute__((packed)), like so: > >struct foo >{ > char a __attribute__((packed)); > long b __attribute__((packed)); >}; Oh. Now it works, thanks. Only problem is that the structure size still is expanded to a 4-byte boundary; The above structure would have foo::a at offset 0, foo::b at 1, but sizeof(foo) returns 8. But alright, this _is_ mentioned in the GCC manual. (Using the -fpack-struct argument yields sizeof(foo) = 5, however.) >> "packed: >> This attribute, attached to an enum, struct, or union type definition, >> [...] > >Note that the above excerpt is from a Chapter called "C Extensions", not >"C++ Extensions". Oops. I incorrectly assumed that C and C++ programs behaves identically. Sorry.