Initializing Static and Global Variables
5-12
5.7 Initializing Static and Global Variables
The ANSI C standard specifies that static and global (extern) variables without
explicit initializations must be initialized to 0 before the program begins
running. This task is typically performed when the program is loaded. Because
the loading process depends heavily on the specific environment of the target
application system, the compiler itself makes no provision for preinitializing
variables. It is up to your application to fulfill this requirement.
If your loader does not preinitialize variables, you can use the linker to prein-
itialize the variables to 0 in the object file. For example, in the linker command
file, use a fill value of 0 in the .bss section:
SECTIONS
{
...
.bss: {} = 0x00;
...
}
Because the linker writes a complete load image of the zeroed .bss section
into the output COFF file, this method can have the unwanted effect of
significantly increasing the size of the output file.
5.7.1 Initializing Static and Global Variables With the const Type Qualifier
Static and global variables with the type qualifier
const
are handled differently
than other types of static and global variables.
The
const
static and global variables without explicit initializations are similar
to other static and global variables because they may not be preinitialized to
0 (for the same reasons discussed above). For example:
const int zero;
/* may not be initialized to zero */
However, const, global, and static variables’ initializations are different be-
cause they are declared and initialized in a section called .const. For example:
const int zero = 0;
/*
guaranteed to be zero
*/
which corresponds to an entry in the .const section:
.sect
.const
_zero
.word
0
The feature is particularly useful for declaring a large table of constants
because neither time nor space is wasted at system startup to initialize the
table. Additionally, the linker can be used to place the .const section in ROM.
Summary of Contents for TMS320C2x
Page 8: ...viii...
Page 69: ...2 47 C Compiler Description...
Page 159: ...6 36...
Page 226: ...8 6...