How to specify a memory location at which function

2019-06-02 23:57发布

Can anyone please let me know what are the possible ways in which we can store a function(in C) at a fixed memory location.

I am using IAR IDE for ARM cortex m3 core.

1条回答
来,给爷笑一个
2楼-- · 2019-06-03 00:48

How functions and data can be placed in memory is described in section "Controlling data and function placement in memory" of the IAR manual "IAR C/C++ Development Guide".

The only way I see to place a specific function at a specific address is to define a section in memory where to place this and only this function. Example:

void MyFunction( void ) @ "MyFunctionsSection"
{
...
}

In the linker file you have to define the section "MyFunctionsSection". There you can define where this section should be placed. Your function will then be placed there.

The disadvantage of this method is that you have to reserve memory for this function. So you must know the size of the function and have to adapt this memory section if the size of the function increases and gets bigger than the reserved memory. Another drawback is that you can only place one function in this section to be sure that your function is placed at the wished address.

But besides this: The idea to place a function at a specific address is generally odd. Better would be to define a pointer to this function and place the pointer at a fix address as it is done in interrupt vector tables. If you have more function pointers the list can easily be expanded.

查看更多
登录 后发表回答