Interesting problem dynamically forming func names ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • onkar.n.mahajan@gmail.com

    Interesting problem dynamically forming func names ??

    Is it possible to from function call on fly in C programming
    language ?

    I am faced with an interesting problem in that I need to take function
    name and arguments on fly (actually from Database as string ) and form
    a function call based on that ?


    something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg 3" , for this
    pair function call must be
    fun1(arg1,arg2, arg3);

    assume that fun1(int,char,c har*) is defined before the call ?

    i.e.,

    get fun_name,args;
    form a call



    Regards,
    Onkar







  • Antoninus Twink

    #2
    Re: Interesting problem dynamically forming func names ??

    On 20 Jun 2008 at 9:29, onkar.n.mahajan @gmail.com wrote:
    Is it possible to from function call on fly in C programming
    language ?
    >
    something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg 3" , for this
    pair function call must be
    fun1(arg1,arg2, arg3);
    The main choices are: maintain a table of function pointers and hash
    FUN_NAME to an index in the table, or use a function like dlsym() to
    access the symbol table itself.

    Comment

    • rahul

      #3
      Re: Interesting problem dynamically forming func names ??

      On Jun 20, 2:29 pm, onkar.n.maha... @gmail.com wrote:
      Is it possible to from function call on fly in C programming
      language ?
      >
      I am faced with an interesting problem in that I need to take function
      name and arguments on fly (actually from Database as string ) and form
      a function call based on that ?
      >
      something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg 3" , for this
      pair function call must be
      fun1(arg1,arg2, arg3);
      >
      assume that fun1(int,char,c har*) is defined before the call ?
      >
      i.e.,
      >
      get fun_name,args;
      form a call
      >
      Regards,
      Onkar
      /* Not compiled */
      #include <stdio.h>
      #include <stdlib.h>

      #define NAME_LEN ..
      #define NUM ..

      struct _node {
      char name[NAME_LEN];
      void (*fnPtr)(void);
      }node;

      int foo(int a);
      void bar(char *a, float b);
      double baz(void );

      int
      main(void) {
      struct node table[NUM] = { {"foo", &foo}, {"bar", &bar}, {"baz",
      &baz} };
      char name[NAME_LEN] = getNameFromData base();
      int count = 0;
      for ( ; count < NUM; count++) {
      if (strcmp(name, table.name) == 0) {
      table.fnPtr(/*place the arguments */);
      }
      }
      return 0;
      }


      This is a very crude approach. You can refine on this( use link list
      or hash tables in place of arrays, do something for parameters etc)
      but the basic framework is going to be the same.

      There aren't any generic function pointers in C (void * can stand only
      for objects) but a pointer to a function can stand for any other
      function type. You can use some casting to shut the compiler up.

      Comment

      • Bartc

        #4
        Re: Interesting problem dynamically forming func names ??


        <onkar.n.mahaja n@gmail.comwrot e in message
        news:6f4a5bbf-c665-4320-b27c-0ada08450ba0@k3 7g2000hsf.googl egroups.com...
        Is it possible to from function call on fly in C programming
        language ?
        >
        I am faced with an interesting problem in that I need to take function
        name and arguments on fly (actually from Database as string ) and form
        a function call based on that ?
        >
        >
        something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg 3" , for this
        pair function call must be
        fun1(arg1,arg2, arg3);
        >
        assume that fun1(int,char,c har*) is defined before the call ?
        A table of function names and addresses has been mentioned.

        If you are using Windows and the functions are in a DLL file, then they are
        often accessed by name anyway:

        GetProcAddress( hinst,"ijlInit" )

        and perhaps there might be something similar on your system.

        If the functions could have different parameter sets and return values,
        that's an extra complication; you will need to classify them according to
        their signatures and call each group separately.

        How many functions are we talking about? The function name cannot be
        arbitrary because it must already exist somewhere (in most languages not
        just C).

        --
        Bartc



        Comment

        • Wolfgang Draxinger

          #5
          Re: Interesting problem dynamically forming func names ??

          onkar.n.mahajan @gmail.com wrote:
          Is it possible to from function call on fly in C programming
          language ?
          >
          I am faced with an interesting problem in that I need to take
          function name and arguments on fly (actually from Database as
          string ) and form a function call based on that ?
          >
          >
          something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg 3" ,
          for this pair function call must be
          fun1(arg1,arg2, arg3);
          >
          assume that fun1(int,char,c har*) is defined before the call ?
          The one problem is getting the address of the function code by
          only the name, the other problem is building an arbitrary
          function call at runtime.

          The first problem is addressed by dynamic linkers: If your
          program exports it's symbols you can do a dlsym (*nix) or
          GetProcAddress (Windows) on it. Or you add some symbol table at
          compile time

          typedef struct symbol
          {
          char const * const name;
          void * const p;
          } symbol;
          symbol symbols[] =
          {
          {"bla", bla},
          {"foo", foo},
          {"bar", bar},
          ...
          };

          The second one _can not_ be gernerically addressed with C alone!
          Although you can define a whole bunch of function prototype
          types and use some very large switch structure to implement it
          to some degree. It is however in this case a lot more easier -
          if you know the architecture - to dynamically build the call
          frame using assembler. And since such is a usefull thing for
          e.g. script language interpreters, someone has implemented it
          and provides it as a part of the ffcall library:



          HTH

          Wolfgang Draxinger
          --
          E-Mail address works, Jabber: hexarith@jabber .org, ICQ: 134682867

          Comment

          • vippstar@gmail.com

            #6
            Re: Interesting problem dynamically forming func names ??

            On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.co mwrote:
            <onkar.n.maha.. .@gmail.comwrot e in message
            >
            news:6f4a5bbf-c665-4320-b27c-0ada08450ba0@k3 7g2000hsf.googl egroups.com...
            >
            Is it possible to from function call on fly in C programming
            language ?
            It's not possible. There are workarounds, Mr rahul posted an example
            of one.
            <snip>
            A table of function names and addresses has been mentioned.
            The person who mentioned it is a troll (just so there isn't a
            misunderstundin g: I'm talking about Antoninus Twink). Don't advice
            people to look at posts made my trolls.
            If you are using Windows and the functions are in a DLL file, then they are
            often accessed by name anyway:
            >
            GetProcAddress( hinst,"ijlInit" )
            Which is not topical in comp.lang.c. Please stop posting off-topic
            advice.
            <snip>

            Comment

            • Bartc

              #7
              Re: Interesting problem dynamically forming func names ??

              <vippstar@gmail .comwrote in message
              news:e3a1703b-94ef-40c9-ad6d-936046802154@k3 7g2000hsf.googl egroups.com...
              On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.co mwrote:
              ><onkar.n.maha. ..@gmail.comwro te in message
              >>
              >news:6f4a5bb f-c665-4320-b27c-0ada08450ba0@k3 7g2000hsf.googl egroups.com...
              >>
              Is it possible to from function call on fly in C programming
              language ?
              It's not possible. There are workarounds, Mr rahul posted an example
              of one.
              <snip>
              >A table of function names and addresses has been mentioned.
              The person who mentioned it is a troll (just so there isn't a
              misunderstundin g: I'm talking about Antoninus Twink). Don't advice
              people to look at posts made my trolls.
              Nevertheless, his advice (also repeated in the thread) was good.
              >If you are using Windows and the functions are in a DLL file, then they
              >are
              >often accessed by name anyway:
              >>
              >GetProcAddress (hinst,"ijlInit ")
              Which is not topical in comp.lang.c. Please stop posting off-topic
              advice.
              Just trying to help. And I said "if".

              Anyway this was also mentioned in this thread (by Wolfgang Draxinger), but
              strangely you have not ticked him off.

              So why pick on me?

              --
              Bartc


              Comment

              • vippstar@gmail.com

                #8
                Re: Interesting problem dynamically forming func names ??

                On Jun 20, 4:22 pm, "Bartc" <b...@freeuk.co mwrote:
                <vipps...@gmail .comwrote in message
                >
                news:e3a1703b-94ef-40c9-ad6d-936046802154@k3 7g2000hsf.googl egroups.com...
                >
                On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.co mwrote:
                <onkar.n.maha.. .@gmail.comwrot e in message
                >
                >news:6f4a5bb f-c665-4320-b27c-0ada08450ba0@k3 7g2000hsf.googl egroups.com...
                >
                Is it possible to from function call on fly in C programming
                language ?
                It's not possible. There are workarounds, Mr rahul posted an example
                of one.
                <snip>
                A table of function names and addresses has been mentioned.
                The person who mentioned it is a troll (just so there isn't a
                misunderstundin g: I'm talking about Antoninus Twink). Don't advice
                people to look at posts made my trolls.
                >
                Nevertheless, his advice (also repeated in the thread) was good.
                It was not, Antoninus also mentioned dlsym, which is not ISO C.
                Antoninus doesn't care about advice, he just wants to post off-topic
                crap to disrupt the newsgroup.
                If you are using Windows and the functions are in a DLL file, then they
                are
                often accessed by name anyway:
                >
                GetProcAddress( hinst,"ijlInit" )
                Which is not topical in comp.lang.c. Please stop posting off-topic
                advice.
                >
                Just trying to help. And I said "if".
                I understand your intentions were genuine but still, please try to
                avoid posting off-topic advice.
                "if" is not the same with "The following is not topical here in
                comp.lang.c and the ISO C standard does not mention such function",
                who knows what OP might think...
                Anyway this was also mentioned in this thread (by Wolfgang Draxinger), but
                strangely you have not ticked him off.
                >
                So why pick on me?
                Actually I did not see Mr Draxingers post, but now that I read it, I
                shall advice him the same, to not post off-topic replies/help.
                It's best to remain topical, it keeps trolling noise to a minimum.

                Comment

                • Richard

                  #9
                  Re: Interesting problem dynamically forming func names ??

                  vippstar@gmail. com writes:
                  On Jun 20, 4:22 pm, "Bartc" <b...@freeuk.co mwrote:
                  ><vipps...@gmai l.comwrote in message
                  >>
                  >news:e3a1703 b-94ef-40c9-ad6d-936046802154@k3 7g2000hsf.googl egroups.com...
                  >>
                  On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.co mwrote:
                  ><onkar.n.maha. ..@gmail.comwro te in message
                  >>
                  >>news:6f4a5b bf-c665-4320-b27c-0ada08450ba0@k3 7g2000hsf.googl egroups.com...
                  >>
                  Is it possible to from function call on fly in C programming
                  language ?
                  It's not possible. There are workarounds, Mr rahul posted an example
                  of one.
                  <snip>
                  >A table of function names and addresses has been mentioned.
                  The person who mentioned it is a troll (just so there isn't a
                  misunderstundin g: I'm talking about Antoninus Twink). Don't advice
                  people to look at posts made my trolls.
                  >>
                  >Nevertheless , his advice (also repeated in the thread) was good.
                  It was not, Antoninus also mentioned dlsym, which is not ISO C.
                  Antoninus doesn't care about advice, he just wants to post off-topic
                  crap to disrupt the newsgroup.
                  >If you are using Windows and the functions are in a DLL file, then they
                  >are
                  >often accessed by name anyway:
                  >>
                  >GetProcAddress (hinst,"ijlInit ")
                  Which is not topical in comp.lang.c. Please stop posting off-topic
                  advice.
                  >>
                  >Just trying to help. And I said "if".
                  I understand your intentions were genuine but still, please try to
                  avoid posting off-topic advice.
                  Get a life you miserable jobs worth. Your attempts at cosying up to the
                  clique are quite nauseating. If the replies give HELP then all well and
                  good. It might surprise you but a LOT of people coming to this group
                  dont give a rats arse about having C code which can compile on 20000
                  different systems. They are C programmers looking for some help. If
                  there is an alternative ISO compliant solution then why do you not give
                  it? Probably because like Default User and "Chuck" more of your posts
                  are pedantic nit picking and net nannying than offering any real advice.
                  "if" is not the same with "The following is not topical here in
                  comp.lang.c and the ISO C standard does not mention such function",
                  who knows what OP might think...
                  >Anyway this was also mentioned in this thread (by Wolfgang Draxinger), but
                  >strangely you have not ticked him off.
                  >>
                  >So why pick on me?
                  Actually I did not see Mr Draxingers post, but now that I read it, I
                  Aha. The old "Mr". Clearly we have a nym shifter in our midst.
                  shall advice him the same, to not post off-topic replies/help.
                  It's best to remain topical, it keeps trolling noise to a minimum.
                  It would bet better for a lot of people if you would keep you trolling
                  net nannying down too.

                  Comment

                  • soscpd

                    #10
                    Re: Interesting problem dynamically forming func names ??

                    On Jun 20, 5:29 am, onkar.n.maha... @gmail.com wrote:
                    I am faced with an interesting problem in that I need to take function
                    name and arguments on fly (actually from Database as string ) and form
                    a function call based on that ?
                    Hello Onkar, List

                    Can you please, elaborate? Where are that function's when you first
                    compile the application? It is from some dynamic lib? Did you consider
                    use some embedded script language, or maybe embed some c compiler to
                    run the code on the fly? You *NEED* to do that, as assignment, or you
                    just think that is the solution to your problem (and really... so far,
                    I dont know what problem is it)? Maybe we can change your question a
                    little bit, to find a better answer... :o

                    Regards
                    Rafael

                    Comment

                    • Nick Keighley

                      #11
                      Re: Interesting problem dynamically forming func names ??

                      On 20 Jun, 12:29, "Bartc" <b...@freeuk.co mwrote:

                      <snip>
                      The function name cannot be
                      arbitrary because it must already exist somewhere (in most languages not
                      just C).
                      but not all. Or rather it doesn't have to exist at compile time.

                      --
                      Nick Keighley

                      Comment

                      • YarvinG Liu

                        #12
                        Re: Interesting problem dynamically forming func names ??

                        On Jun 20, 5:29 pm, onkar.n.maha... @gmail.com wrote:
                        Is it possible to from function call on fly in C programming
                        language ?
                        >
                        I am faced with an interesting problem in that I need to take function
                        name and arguments on fly (actually from Database as string ) and form
                        a function call based on that ?
                        >
                        something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg 3" , for this
                        pair function call must be
                        fun1(arg1,arg2, arg3);
                        >
                        assume that fun1(int,char,c har*) is defined before the call ?
                        >
                        i.e.,
                        >
                        get fun_name,args;
                        form a call
                        >
                        Regards,
                        Onkar
                        I've thought about a problem like this.
                        it is my high school career when i was studying pascal language.
                        that problem is i wanna such fun1(array[1..n])
                        the n is variable.but according pascal law, the variable couldn't used
                        in arrays.
                        but in a function(*), i could make n a concrete number. and make fun1
                        be a function in this fuction(*).

                        it works...

                        Comment

                        Working...