DPRG
DPRG List  



[DPRG] I didn't want this to work: C structure confusion

Subject: [DPRG] I didn't want this to work: C structure confusion
From: Dave Hylands dhylands at gmail.com
Date: Fri Sep 8 15:06:20 CDT 2006

Hi Dale,

Responding to the list this time.

> GCC doesn't seem to mind mixing my metaphors at all:
>
> PINSEL0->p0_0 = GPIO0_0;
> PINSEL0->p0_1 = GPIO0_0; // <-- not supposed to be legal!

This is perfectly legal in C, but not legal in C++. Sorry, but that's
the way things are.

I HIGHLY recommend that you NOT use bitfields. I've been working in
the embedded arena for almost 20 years, and my experience is that bit
fields just aren't worth the hassle.

The order of the bitfields within the byte/int/whatever is
implementation dependant. You can't rely on bitfield layout working
from one compiler to the next. It's even legal for a compiler vendor
to change the layout from one version of the compiler to another and
still be perfectly compliant with the C/C++ specification.

Personally, I also don't like bit numbers and tend to prefer to use
bitmasks. This are VERY portable.

typedef enum
{
      GPIO0_1  = 0x01,
      RXD0             = 0x02,
      MAT3_2   = 0x04,
} PCB0_1;


> Can anyone educate me as to why the compiler is not restricting the
> assignment of wrong-type values to my bitfields?  I thought that was
> what typedef'ing was for.

In C enums and ints are considered equal in all respects. And even in
C++, I'm not sure what bitfield enums would mean.

-- 
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

More information about the DPRG mailing list

Copyright © 1984 - 2006 Dallas Personal Robotics Group. All rights reserved.
Website Design by NCC

For the latest robot news visit robots.net