Thread: with Function

  1. #1
    Registered User
    Join Date
    Nov 2009

    with Function

    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...

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Maybe. Depends on the tools, system architecture, and perhaps even the embedded OS being used.


  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Quote Originally Posted by vijay s View Post
    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...
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008

    Use function pointers

    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

    typedef funcptr_t int (*func)(int a, int b);
    then you have the type funcptr which you can use, for example in a struct

    typedef struct {
        int a; // Just an integer
        funcptr_t someFunc;
    }funcPtrContainer_t, *funcPtrContainer_t;
    The you declear a function to use

    int someFuncThatDoSomething(int oneVar, int twoVar)
         // Function body
         return 0;
    In the program you just, declear the following;

    funcptr_t funcPtr = NULL;
    funcPtrContainer_t funcPtrCont;
    funcPtr = someFuncThatDoSomething;
    funcPtrCont.someFunc = someFuncThatDoSomething;
    Then you call them some where in the code, in either way:

    int result = 0;
    result = funcPtr(1, 4);
    result = (*funcPtr)(1, 4);
    result = funcPtrCont.someFunc(1, 4);
    result = (*funcPtrCont.someFunc)(1, 4);
    Which all yield the same result.

    Function pointer are perticulary useful in real applications where runtime binding is needed, an is basically the early idea behind ploymorphism in OO languages.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM