Bug in gcc?

Sergei Organov osv at javad.com
Thu Jun 5 16:55:05 UTC 2008


Leon Pollak <leonp at plris.com> writes:

> Hello, all.
>
> It seems to me I found a bug in gcc, but as it is rtems-provided cross-gcc, I 
> think it was better to ask here first.
>
> I have the following code:
> template <class TYPE> class sCmnd2DSPs {
> public:
> 	enum eSources {eDiscr=0, eRS1, eRS2, 
> 		eMB1, eMB2, eEth1, eEth2} __attribute__ ((packed));
> 	struct {
> 		eSources	Source;
> 		uchar		Addr;
> 		eCmndCodesDSPs	Opcode;
> 		TYPE		Params; } __attribute__ ((packed));
> ...............
> };
>
> Everything works fine, but the field Source in the code above occupies 4 
> bytes, despite the packed request.
>
> Now, if I take the enumeration declaration out of the class (put it above the 
> class declaration), then the Source field occupies 1 byte as required.
>
> Should I report this to gcc list?

Well, probably it's a GCC bug, but if it's not GCC 4.2 or later, I'm
afraid it's pointless to report this to gcc list.

As a side note, every time I see __attribute__((packed)) in the code, I
think "somebody is looking for troubles" ;) Did you consider to re-write
your code not to use such hacks?

A solution to your particular problem could be declaring 'Source' a
bit-field (untested):

  enum eSources {eDiscr=0, eRS1, eRS2, 
   eMB1, eMB2, eEth1, eEth2} __attribute__ ((packed));
  struct {
  	eSources	Source : 8;
  	uchar		Addr;
  	eCmndCodesDSPs	Opcode;
  	TYPE		Params; }  __attribute__ ((packed));

        
-- Sergei.



More information about the users mailing list