Ambiguous C?
Andrew Koenig
ark at alice.UUCP
Fri Apr 28 00:25:30 AEST 1989
In article <111 at ssp1.idca.tds.philips.nl>, roelof at idca.tds.PHILIPS.nl (R. Vuurboom) writes:
> The following piece of code got me into trouble.
> I needed to access a register which only allows long accesses.
. . .
> Does C specify which (if any) interpretation is correct?
> #define SCUCMD 0
> #define SCU_BDID 0x2000 /* bit 14 */
> if ( (*(int *)(SCUCMD)) & SCU_BDID )
There are two separate questions:
1. Does C define the meaning of this program fragement?
2. How do you get the compiler to do what you want?
The answer to (1) is `no' -- once you cast an integer into
a pointer, you're on your own.
In practice, what's probably happening is that the compiler
realizes that there's only one bit turned on in the value
of SCU_BDID, so it's optimizing the code by not fetching
the entire word to test.
To suppress the optimization, you can probably conceal the
value of SCU_BDID from the compiler by using it in a way that
the compiler thinks might change. For instance:
static int SCU_BDID = 0x2000;
Now the compiler probably won't be smart enough to realize that
SCU_BDID is really a constant, so it will have to generate code
that caters to any bit in the whole word being on.
--
--Andrew Koenig
ark at europa.att.com
More information about the Comp.lang.c
mailing list