Microchip Technology SW006023-2N Data Sheet

Page of 238
MPLAB
®
 XC32 C/C++ Compiler User’s Guide
DS51686E-page 114
 2012 Microchip Technology Inc.
7.5.1
Size Limitations of 
const
 Variables
There is no theoretical maximum size for const variables.
7.5.2
Changing the Default Allocation
If you only intend to prevent all variables from using one or more program memory loca-
tions so that you can use those locations for some other purpose, you are best reserv-
ing the memory using the memory adjust options.
If only a few non-auto const variables are to be located at specific addresses in 
program space memory, then the variables should use the address attribute to locate 
them at the desired location. This attribute is described in Section 6.12 “Variable 
Attributes”
.
7.6
VARIABLES IN REGISTERS
Allocating variables to registers, rather than to a memory location, can make code more 
efficient. With MPLAB XC32 C/C++ Compiler, variables may be allocated to registers 
as part of code optimizations. For optimization levels 1 and higher, the values assigned 
to variables may cached in a register. During this time, the memory location associated 
with the variable may not hold a valid value.
The register keyword may be used to indicate your preference for the variable to be 
allocated a register, but this is just a recommendation and may not be honored. The 
specific register may be indicated as well, but this is not recommended as your register 
choice may conflict with the needs of the compiler. For example:
register unsigned int foo __asm__("at")
;
will attempt to allocate foo to the at register. As indicated in Section 10.6 “Function 
Parameters”
, par
ameters may be passed to a function via a register. 
7.7
DYNAMIC MEMORY ALLOCATION
The run-time heap is an uninitialized area of data memory that is used for dynamic 
memory allocation using the standard C library dynamic memory management 
functions,  calloc, malloc and realloc along with the C++ new operator. Most C++ 
applications will require a heap.
If you do not use any of these functions, then you do not need to allocate a heap. By 
default, a heap is not created.
If you do want to use dynamic memory allocation, either directly, by calling one of the 
memory allocation functions, or indirectly, by using a standard C library function that 
uses one of these functions, then a heap must be created. A heap is created by 
specifying its size on the linker command line using the --defsym_min_heap_size 
linker command line option. An example of allocating a heap of 512 bytes using the 
command line is:
xc32-gcc foo.c -Wl,--defsym,_min_heap_size=512
An example of allocating a heap of 0xF000 bytes using the xc32-g++ driver is:
xc32-g++ vector.cpp -Wl,--defsym,_min_heap_size=0xF000
The linker allocates the heap immediately before the stack.
7.8
MEMORY MODELS
MPLAB XC32 C/C++ Compiler does not use fixed memory models to alter allocation of 
variables to memory.