11.2.2.4 Changing Non-Auto Variable Allocation

The compiler arranges for data to be placed into sections, depending on the memory model used and whether or not the data is initialized, as described in the 11.1 Address Spaces section. When modules are combined at link time, the linker determines the starting addresses of the various sections based on their attributes.

Cases may arise when a specific variable must be located at a specific address, or within some range of addresses. The easiest way to accomplish this is by using the address attribute, described in sectionDifferences Between MPLAB® XC-DSC and C Standard. For example, to locate variable Mabonga at address 0x1000 in data memory:

int __attribute__ ((address(0x1000))) Mabonga = 1;

A group of common variables may be allocated into a named section, complete with address specifications:

int __attribute__ ((section("mysection"), address(0x1234))), foo;