Constant function pointer in On-chip Flash


#include "LPC177x_8x.H"

void f1(U8 *ptr);
void f2(U8 *ptr);
void (*fpTest1[2])(U8 *ptr1)={&f1,&f2};
void (*const fpTest[2])(U8 *ptr1)={&f1,&f2};
U8 b[2][2]={{0,1},{2,3}};

int main(void)
{
        (*fpTest[0])(b[0]);
   // fpTest[0]=&f2;
   //(*fpTest[0])(b[0]);
   (*fpTest[1])(b[1]);
}

void f1(U8 *ptr)
{
    volatile U8 a=0;
    volatile U8 b=0;
    a=*(ptr);
    b=*(ptr+1);

}

void f2(U8 *ptr)
{
    volatile U8 a=0;
    volatile U8 b=0;
    a=ptr[0];
    b=ptr[1];

}



Why does fpTest exist in On-Chip Flash and fpTest1 in SRAM?

Parents
  • An initialized variable needs to have the initial value stored in flash. And on boot, the startup code needs to copy this initial value into the RAM variable. So both flash space and RAM space gets consumed.

    If the variable is "const", then the compiler knows that the program isn't allowed to change the initial value. So no need to copy any data into RAM - the program can just as well have the "variable" stored directly in flash. So for a 4-byte pointer, you save 4 byte RAM.

Reply
  • An initialized variable needs to have the initial value stored in flash. And on boot, the startup code needs to copy this initial value into the RAM variable. So both flash space and RAM space gets consumed.

    If the variable is "const", then the compiler knows that the program isn't allowed to change the initial value. So no need to copy any data into RAM - the program can just as well have the "variable" stored directly in flash. So for a 4-byte pointer, you save 4 byte RAM.

Children
More questions in this forum