11.11.1 Gaps Between Variables of Different Types

Gaps may be inserted between variables of different types to satisfy address alignment requirements. For example, the following sequence of C statements will result in a gap:

char c1;
int i;
char c2;
int j;

Because the processor requires integers to be aligned on a 16-bit address boundary, a padding byte was inserted after variables c1 and c2. To eliminate this padding, variables of the same type should be defined together, as shown:

char c1,c2;
int i,j;

Gaps between variables are not visible to the linker and are not reported in the link map. To detect these gaps, an assembly listing file must be created. The following 
procedure can be used:

  • If the source file is written in C, specify the -save-temps command line option to the compiler. This will cause an assembly version of the source file to be saved in filename.s.

    xc-dsc-gcc test.c -save-temps

  • Specify the -ai listing option to the assembler. This will cause a table of section information to be generated.

xc-dsc-as test.s -ai

SECTION INFORMATION:

Section        Length (PC units)   Length (bytes) (dec)
-------        -----------------   --------------------
.text                          0               0  (0)

TOTAL PROGRAM MEMORY USED (bytes):             0  (0)

Section           Alignment Gaps   Length (bytes) (dec)
-------           --------------   --------------------
.data                          0               0  (0)
.bss                           0               0  (0)
.nbss                        0x2             0x8  (8)

   TOTAL DATA MEMORY USED (bytes):           0x8  (8)

In this example, 2 bytes of unused memory were inserted into section .nbss. Gaps between ordinary C variables will not exceed 1 byte per variable.