|
Using the Const Modifier
by
George Martin
Start ý Define
and Qualify ý In Other Words ý Sources
and PDF
IN OTHER WORDS
While weýre here, what other keywords
might be of the same sort of use? Volatile comes to mind. The volatile
modifier warns the compiler that there may be some special hardware
behind the variable causing it to unexpectedly change (see
Listing 5).
In Listing 5, two variables, a
and b, are declared. a is set to 7 and then tested.
A good compiler would know that a is in fact equal to 7, so
no code would be generated for the test and the print() statement
would always be executed. The variable b is declared as volatile,
so even though it was just set to 9, b would be loaded
and tested before the print() statement is reached. If b
was the serial port status register and a was the serial port
control register, in a memory-mapped scheme they could both be at
the same addressýone a read variable and one a write variable. There
is hardware behind that common address that could change the values,
so always reload it.
Although this example may be obvious,
perhaps you can go up another level of optimization on the compiler
now and it will produce stable code!
PREVIOUS
NEXT
Circuit Cellar provides up-to-date information for engineers. Visit
www.circuitcellar.com for
more information and additional articles.
For subscription information, call (860) 875-2199, subscribe@circuitcellar.com
or subscribe online.
ýCircuit Cellar, the Magazine for Computer Applications. Posted with
permission. |