9.3 Auto Variable Allocation and Access

This section discusses allocation of auto variables (those with automatic storage duration). This also includes function parameter variables, which behave like auto (short for automatic) variables, as well as temporary variables defined by the compiler.

The auto variables are the default type of local variable. Unless explicitly declared to be static, a local variable will be made auto. The auto keyword may be used if desired.

The auto variables, as their name suggests, automatically come into existence when a function is executed, then disappear once the function returns. Since they are not in existence for the entire duration of the program, there is the possibility to reclaim memory they use when the variables are not in existence and allocate it to other variables in the program.

On PIC32C devices, the registers r4-r8, r10, and r11 are used to hold the values of a function's automatic variables. A function must preserve the contents of the registers r4-r8, r10, r11, and r13/SP.

Often times, the software stack of the PIC32C is used to store auto variables. The values from registers are pushed onto the stack. Functions are reentrant and each instance of the function has its own area of memory on the stack for its auto and parameter variables.

On PIC32C devices, the Stack Pointer (SP) is register r13. The core uses a full descending stack. A full descending stack means that the stack pointer holds the address of the last stacked item in memory and the stack grows to lower addresses. When the core pushes a new item onto the stack, it decrements the stack pointer and then writes to the item to the new memory location.

The standard qualifiers const and volatile may both be used with auto variables and these do not affect how they are positioned in memory. This implies that a local const-qualified object is still an auto object and, as such, will be allocated memory on the stack in the data memory, not in the program memory like with non-auto const objects.