SPRUI04F july 2015 – april 2023
The C/C++ compiler supports the ANSI/ISO standard keyword const in all modes. This keyword gives you greater optimization and control over allocation for certain data objects. You can apply the const qualifier to the definition of any variable or array to ensure that its value is not altered.
Global objects qualified as far const are placed in the .const section. The linker allocates the .const section from ROM or FLASH, which are typically more plentiful than RAM. The const data storage allocation rule has the following exceptions:
volatile const int
x
. Volatile keywords are assumed to be allocated to RAM. (The
program is not allowed to modify a const volatile object, but something external
to the program might.)In these cases, the storage for the object is the same as if the const keyword were not used.
The placement of the const keyword is important. For example, the first statement below defines a constant pointer p to a modifiable int. The second statement defines a modifiable pointer q to a constant int:
int * const p = &x;
const int * q = &x;
Using the const keyword, you can define large constant tables and allocate them into system ROM. For example, to allocate a ROM table, you could use the following definition:
far const int digits[] = {0,1,2,3,4,5,6,7,8,9};