4.6.6.12 Zeroallram Option

The -fzeroallram=off|on|auto option controls which regions of data memory are cleared by the runtime startup code. This feature is only available for Enhanced Mid-range and PIC18 devices.

As required by the C standard, uninitialized objects with permanent storage duration must be zeroed before main() executes. Most 8-bit PIC devices have RAM divided into multiple data banks, and uninitialized objects could be located in many of these banks. Adjusting how these memory regions are cleared can lead to code size savings, which is the purpose of this option. Note, however, that this option cannot be used to disable the clearing of uninitialized objects altogether. Use the -Wl,--no-data-init option for that purpose.

When the option -fzeroallram=off has been issued, the compiler will generate start up code that clears only the memory occupied by the uninitialised objects. This might require the use of several loops to complete this task if the objects are present in more than one data bank; however, the startup code will run fast.

When the argument to the -fzeroallram is specified as on and the optimization level is set at -O3 or -Os, the compiler will instead zero the entire amount of RAM present on the target device, regardless of where the uninitialised objects have been allocated. The code to perform this will be typically smaller than that required to clear discrete ranges; however, it might take longer to execute. Use this feature with caution if your project contains __persistent variables, as these are often assumed to hold their value over a reset. An advisory message will be issued if __persistent variables are present when this feature has been enabled. A message will also be issued if you attempt to enable this feature when the optimization level is set to below -O3. You might choose to enable this optimization for functional safety reasons or to assist with debugging.

When the argument to this option is set to auto, the compiler will zero the entirety of data memory only if it results in smaller code and the following are true.
  • Level -Os optimizations have been selected
  • There are no objects qualified __persistant
  • There are no assembly modules and there are no variables defined in inline assembly
  • The -mram option has not been used to modify the default RAM configuration in any way
  • Neither the -mcodeoffset nor -mmaxichip option have been used
The auto setting is the default selection made by the compiler if this option has not been specified.