#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?
Thanks.
So even if the const pointer tries to point to an invalid address(address of function not assigned for it to point to) by accident while executing the code from Flash, it will cause an exception ?
Different rules for different processors what memory regions that may produce access violations. It's the memory controller "glue" that decides if a memory region has write protection or both read+write protection or if you are free to both read and write. So the memory controller can allow the program to make read and write accesses even to ranges where there are no memory - it's just depends on what specific processor that is used. And in some cases on what register initializations your program has made.
But besides violations from accessing invalid memory ranges, you can also get exceptions by trying to execute invalid instructions. Which may happen if a pointer points into "air". Or if you get a stack overflow, or the program has problems with memory overwrites. Or maybe there are problems with the memory so you get bit errors.
Note that you do not need to perform any assign to the pointer - the compiler/linker together with the startup code will assign initial values to the pointers. And as long as you don't upload a broken binary to the processor, there will be two functions at the destination addresses for the two pointers.