delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/05/18:21:28

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: <Pine DOT SUN DOT 3 DOT 91 DOT 970105165834 DOT 22565A-100000 AT is>
NNTP-Posting-Host: x6.communique.se
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <Pine DOT SUN DOT 3 DOT 91 DOT 970105165834 DOT 22565A-100000 AT is>, 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.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019