Keil Logo

In a ROM Variable

When a function pointer is assigned to a global variable that is stored in ROM (CODE), the call tree generated by the linker may be completely accurate. For example, given the following example program:

int direct_func (int a, int b, int c)
volatile int total = a+b+c;
return (total);

int indirect_func (int a, int b, int c)
volatile int total = a+b+c;
return (total);

int (* code fp) (int, int, int) = indirect_func;

int caller (void)
int retval;

retval = direct_func(4,5,6);
retval += (*fp) (1,2,3);
return (retval);

void main (void)
int value;

value = caller();

while (1);

The correct Call Tree is illustrated by the following flow chart.

Correct Call Tree

However, indirect_func is referenced by the code segment of the main source file (?co?main) that contains the function pointer fp. It is not referenced in the caller function. However, the caller function does reference the fp variable. Therefore, the linker generates the following Call Tree and Overlay Map.

Incorrect Call Tree

?C_C51STARTUP                        ----- -----  ----- -----
  +--> ?PR?MAIN?MAIN

MAIN/MAIN                            ----- -----  0008H 0009H

CALLER/MAIN                          ----- -----  000AH 000BH
  +--> ?CO?MAIN

_DIRECT_FUNC/MAIN                    ----- -----  000CH 000DH

?CO?MAIN                             ----- -----  ----- -----

_INDIRECT_FUNC/MAIN                  ----- -----  000CH 000DH

The ?CO?MAIN segment contains all code variables declared in the MAIN.C file. If this consists of only function pointers, all of which are accessed by a C function, then no changes to the call tree are required. Refer to the following rules regarding global function pointers stored in CODE to determine if adjustments to the call tree are required.

  1. Global function pointers assigned to CODE memory may be the only variables declared in a source file.
  2. Function pointers declared must all be used (or have the potential to be used) in the function or functions that reference them.
  3. If some, but not all, function pointers are used in a function, you must manually adjust the call tree to reflect this relationship. Otherwise, precious memory is wasted by the inaccurate call tree.


  • The fp variable is located in CODE memory which is stored in the ?CO?MAIN segment — along with all other global CODE variables declared in MAIN.C.
  • References to any global CODE variable in the MAIN.C source file (even from another source file) is indicated in the call tree by a reference to the ?CO?MAIN segment.
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.