Thread: function pointer question

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14

    function pointer question

    Hello all!

    I have a question on function pointers.

    The name of the function cannot be known a priori, since it is supplied at runtime when my program opens a file and retrieves the name of the function to call.

    The external file will have something like this:
    FUNCTION_NAME = 'SomeFunc'

    I open that file and read the attribute storing it as a string:

    std::string tmpFunc;

    the above is set to the string value extracted from the file.

    The function pointer is defined as:

    void (*fp) (int *);

    one input argument to the function.

    How can I initialize the function pointer? Can I just do this?

    void (*fp) (int *) = tmpFunc.c_str()

    Thanks for the kind help.
    GEoff.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No. The names of functions are only known at compile time. After that, you could perhaps use the exported symbols of dynamic libraries for some tricks, but nothing that works within standard C++.

    However, you can just pre-register some functions, i.e.
    Code:
    typedef void (*fp_t)(int *);
    typedef std::map<std::string, fp_t> fp_map;
    typedef fp_map::value_type fp_mapping;
    
    fp_map fps;
    fps.insert(fp_mapping("func1", &func1));
    fps.insert(fp_mapping("func2", &func2));
    fps.insert(fp_mapping("func3", &func3));
    
    
    // And later look up.
    fp_map::const_iterator ci = fps.find(tmpFunc);
    if(ci != fps.end()) {
      fp_t fp = ci->second;
      fp(data);
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which OS/Compiler ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    On Linux you can dlopen() yourself and then call dlsym() to retrieve symbols, but the symbol names are not easily guessed due to C++ name mangling. It is far easier as well as more portable to just make a table which maps names to function addresses:

    Code:
    // A table of void functions which take single integers:
    typedef void (*funcptr)(int);
    
    struct func_map
    {
        const char *name;
        funcptr func;
    } functions[] =
    {
        { "foobar", &foobar },
        { "asdf", &asdf },
        { "blah", &blah }
    };
    Then you need a function which looks through the array to find a match and returns the appropriate function pointer.

    The problem is that each function pointer must be exactly the same type. If you need a more general method of passing functions around it is better to use templates and functors.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14
    Quote Originally Posted by Salem View Post
    Which OS/Compiler ?
    Primarily on Win32 and I'm using MS VS 2005, but the code will have to be portable.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14
    Quote Originally Posted by CornedBee View Post
    No. The names of functions are only known at compile time. After that, you could perhaps use the exported symbols of dynamic libraries for some tricks, but nothing that works within standard C++.

    However, you can just pre-register some functions, i.e.
    Code:
    typedef void (*fp_t)(int *);
    typedef std::map<std::string, fp_t> fp_map;
    typedef fp_map::value_type fp_mapping;
    
    fp_map fps;
    fps.insert(fp_mapping("func1", &func1));
    fps.insert(fp_mapping("func2", &func2));
    fps.insert(fp_mapping("func3", &func3));
    
    
    // And later look up.
    fp_map::const_iterator ci = fps.find(tmpFunc);
    if(ci != fps.end()) {
      fp_t fp = ci->second;
      fp(data);
    }
    Thanks for your answer.
    A little extra info: when the string is being read from the file, it needs to exist as a valid exported symbol within the dll that will contain this code.
    So if the file says:
    FUNCTION_NAME = 'SomeFunc'
    Then I will manage the name mangling according to platform etc..
    Therefore the actual symbol will be _SomeFunc@4 (if the input argument is a int *) and this is for Windows 32.

    So it is assumed that the function will be defined. I hope this clarifies the problem a little more.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If it's in a DLL, the usual way would be LoadLibrary/GetProcAddress/CloseLibrary.
    These are Windows-only, though AFAIK.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Primarily on Win32 and I'm using MS VS 2005, but the code will have to be portable.
    brewbuck and Elysia have posted what you need to do
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. Function pointer question
    By sbayeta in forum C Programming
    Replies: 9
    Last Post: 08-06-2004, 08:15 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM