Microchip Technology SW006022-2N Data Sheet

Page of 338
Memory Allocation and Access
 2012 Microchip Technology Inc.
DS52071B-page 119
7.3.1.2
STATIC VARIABLES
All static variables have permanent storage duration, even those defined inside a 
function which are “local static” variables. Local static variables only have scope in 
the function or block in which they are defined, but unlike auto variables, their memory 
is reserved for the entire duration of the program. Thus they are allocated memory like 
other non-auto variables. Static variables may be accessed by other functions via 
pointers since they have permanent duration.
Variables which are static are guaranteed to retain their value between calls to a 
function, unless explicitly modified via a pointer.
Variables which are static and which are initialized only have their initial value 
assigned once during the program’s execution. Thus, they may be preferable over ini-
tialized auto objects which are assigned a value every time the block in they are 
defined begins execution. Any initialized static variables are initialized in the same way 
as other non-auto initialized objects by the runtime startup code, see 
Section 3.4.2 “Startup and Initialization”.
7.3.1.3
NON-AUTO VARIABLE SIZE LIMITS
The compiler option -mlarge-arrays allows you to define and access arrays larger 
than 32K. You must ensure that there is enough space to allocate such an array by 
nominating a memory space large enough to contain such an object.
Using this option will have some effect on how code is generated as it effects the defi-
nition of the size_t type, increasing it to an unsigned long int. If used as a global 
option, this will affect many operations used in indexing (making the operation more 
complex). Using this option locally may effect how variables can be accessed. With 
these considerations in mind, using large arrays is requires careful planning. This sec-
tion discusses some techniques for its use.
Two things occur when the -mlarge-arrays option is selected:
1.
The compiler generates code in a different way for accessing arrays.
2.
The compiler defines the size_t type to be unsigned long int.
Item 1 can have a negative effect on code size, if used throughout the whole program. 
It is possible to only compile a single module with this option and have it work, but there 
are limitations which will be discussed shortly.
Item 2 affects the calling convention when external functions receive or return objects 
of type size_t. The compiler provides libraries built to handle a larger size_t and 
these objects will be selected automatically by the linker (provided they exist).
Mixing -mlarge-arrays and normal-sized arrays together is relatively straightfor-
ward and might be the best way to make use of this feature. There are a few usage 
restrictions: functions defined in such a module should not call external routines that 
use size_t, and functions defined in such a module should not receive size_t as a 
parameter.
For example, one could define a large array and an accessor function which is then 
used by other code modules to access the array. The benefit is that only one module 
needs to be compiled with -mlarge-array with the defect that an accessor is 
required to access the array. This is useful in cases where compiling the whole program 
with -mlarge-arrays will have negative effect on code size and speed.