Function Pointers

The MPLAB XC8 compiler fully supports pointers to functions. These are often used to call one of several function addresses stored in a user-defined C array, which acts like a lookup table.

For Baseline and Mid-range devices, function pointers are always one byte in size and hold an offset into a jump table that is output by the compiler. This jump table contains jumps to the destination functions.

For Enhanced Mid-range devices, function pointers are always 16-bits wide and can hold the full address of any function.

For PIC18 devices, function pointers are either 16 or 24 bits wide. The pointer size is purely based on the amount of program memory available on the target device.

As with data pointers, the target assigned to function pointers is tracked. This is an easier process to undertake compared to that associated with data pointers as all function instructions must reside in program memory. The pointer reference graph (described in Pointer Reference Graph) will show function pointers, in addition to data pointers, as well as all their targets. The targets will be names of functions that could possibly be called via the pointer.

One notable runtime feature for Baseline and Mid-range devices is that a function pointer which contains null (the value 0) and is used to call a function indirectly will cause the code to become stuck in a loop which branches to itself. This endless loop can be used to detect this erroneous situation. Typically calling a function via a null function would result in the code crashing or some other unexpected behavior. The label to which the endless loop will jump is called fpbase.