18.2.9 Initialize Objects and RAM Functions

Objects accessed from RAM and functions executed from RAM must have their allocated RAM initialized before execution of the main() function can commence, and this is a significant task performed by the runtime startup code.

Those non-auto objects that are uninitialized must be cleared (assigned the value 0) before execution of main() begins. Such objects are not assigned a value in their definition, for example output in the following example:
int output;
int main(void) { ...
Another task of the runtime start-up code is to ensure that any initialized objects contain their initial value before the program begins execution. Initialized objects are those that are not auto objects and that are assigned an initial value in their definition, for example input in the following example:
int input = 0x88;
int main(void) { ...

Such initialized objects have two components: their initial value (0x0088 in the above example) stored in program memory (that is, placed in the HEX file), and space reserved in RAM, where the objects will reside and be accessed during program execution (runtime).

Four initialized data sections exist: .sdata, .data, .lit4, and .lit8. The .sdata section is a data segment containing initialized objects less than or equal to n bytes, as specified by the -Gn command line option. The .data section is a data segment containing initialized objects not included in .sdata. The .lit4 and .lit8 sections contain constants, (usually floating-point) which the assembler stores in memory rather than in the instruction stream.

The runtime start-up code will copy all the blocks of initial values from program memory to RAM so the objects will contain the correct values before main() is executed.

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 start-up code, but are instead initialized by assembly code in each function output.

Note: Initialized auto objects can impact code performance, particularly if the objects are large in size. Consider using global or static objects instead.

Objects whose contents should be preserved over a Reset should be qualified with the persistent attribute, see 9.9 Standard Type Qualifiers. Such objects are linked at a different area of memory and are not altered by the runtime start-up code in any way.

Any functions that use the ramfunc attribute are copied from program memory to RAM before they are executed. This is also performed by the runtime startup code, in much the same way that initialized objects have their initial value copied before they are accessed.