in Embedded systems point of view...
is there any way to store a function at particular address....
and to run a specified function from RAM...
Printable View
in Embedded systems point of view...
is there any way to store a function at particular address....
and to run a specified function from RAM...
Maybe. Depends on the tools, system architecture, and perhaps even the embedded OS being used.
gg
Functions are generally stored at a particular address. The instructions are read and then it returns to read from the main program.
Do you want to store the function in a specific address, like 0x221A for example? In assembly you can. In C I believe you should also be able to do so, not exactly sure how w/o using assembly lines.
If the OS doesn't let you, it means you should try to do so in the first place.
You can use functions pointers to store references to functions, which will work across code spaces (files, libraries etc). The fuctions pointers can be stored in variables, structures, tables etc. Its just regular pointer type (unsigned int).
On embedded systems you can be fooled by endianess if you cast the int to unsigned chars (as with any other pointer), so be aware. Unlike some other poster in this thread, I do not agree that this is depended os hw architecture or os. This is native C functionallity a can be used on any platform that has native C support, which is basically all.
For declaring a function pointer
then you have the type funcptr which you can use, for example in a structCode:typedef funcptr_t int (*func)(int a, int b);
The you declear a function to useCode:typedef struct {
int a; // Just an integer
funcptr_t someFunc;
}funcPtrContainer_t, *funcPtrContainer_t;
In the program you just, declear the following;Code:int someFuncThatDoSomething(int oneVar, int twoVar)
{
// Function body
return 0;
}
Then you call them some where in the code, in either way:Code:funcptr_t funcPtr = NULL;
funcPtrContainer_t funcPtrCont;
funcPtr = someFuncThatDoSomething;
funcPtrCont.someFunc = someFuncThatDoSomething;
Which all yield the same result.Code:int result = 0;
result = funcPtr(1, 4);
result = (*funcPtr)(1, 4);
result = funcPtrCont.someFunc(1, 4);
result = (*funcPtrCont.someFunc)(1, 4);
Function pointer are perticulary useful in real applications where runtime binding is needed, an is basically the early idea behind ploymorphism in OO languages.
G