Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 195
5.10.1.1
INITIALIZATION OF OBJECTS
One task of the runtime startup code is to ensure that any initialized variables contain 
their initial value before the program begins execution. Initialized variables are those 
which are not auto objects and which are assigned an initial value in their definition, 
for example input in the following example.
int input = 88;
void main(void) { ...
Such initialized objects have two components: their initial value (0x0088 in the above 
example) stored in program memory (i.e., placed in the HEX file), and space for the 
variable reserved in RAM it will reside and be accessed during program execution 
(runtime).
The psects used for storing these components are described in 
Section 5.15.2 “Compiler-Generated Psects”.
The runtime startup code will copy all the blocks of initial values from program memory 
to RAM so that the variables will contain the correct values before main() is executed. 
This action can be omitted by disabling the init suboption of --RUNTIME. For exam-
ple:
--RUNTIME=default,-init
With this part of the runtime startup code absent, the contents of initialized variables 
will be unpredictable when the program begins execution. Code relying on variables 
containing their initial value will fail.
Since auto objects are dynamically created, they require code to be positioned in the 
function in which they are defined to perform their initialization. It is possible that the 
initial value of an auto object may change on each instance of the function and so the 
initial values cannot be stored in program memory and copied. As a result, initialized 
auto
 objects are not considered by the runtime startup code but are instead initialized 
by assembly code in each function output.
Variables whose contents should be preserved over a Reset, or even power off, should 
be qualified with the persistent qualifier, see Section 5.4.8.1 “Persistent Type 
Qualifier”
.
 Such variables are linked at a different area of memory and are not altered 
by the runtime startup code in any way.
If objects are initialized, the runtime startup code which performs this will destroy the 
contents of the STATUS register. With some devices, the TO and PD bits in this register 
are required to determine the cause of Reset. You can choose to have a copy of this 
register taken so that it can later be examined. See Section 5.10.1.4 “STATUS 
Register Preservation”
 
for more information.
5.10.1.2
CLEARING OBJECTS
Those non-auto objects which are not initialized must be cleared before execution of 
the program begins. This task is also performed by the runtime startup code.
Uninitialized variables are those which are not auto objects and which are not 
assigned a value in their definition, for example output in the following example.
int output;
void main(void) {...
Such uninitialized objects will only require space to be reserved in RAM where they will 
reside and be accessed during program execution (runtime).
Note:
Initialized auto variables can impact on code performance, particularly if 
the objects are large in size. Consider using global or static objects 
instead.