Why no generic function pointers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • copx

    Why no generic function pointers?

    Why doesn't the C standard include generic function pointers?

    I use function pointers a lot and the lack of generic ones is not so cool.

    There is a common compiler extension (supported by GCC and lccwin32 for example)
    which I consider to be perfectly reasonable: you can cast every kind of function pointer
    to a void pointer and void pointers to any kind of function pointer.

    This follows the general "generics through void scheme" of C. In fact, it seems to be quite
    "irregular" to me that you can't cast function pointers to void.

    I mean, of course, generic function pointers are "dangerous" , because they allow
    you to call a function with bad arguments and the compiler can't detect that.
    But it's not any more "dangerous" than unsigned char pointers to any type of data i.e.
    not against the "C spirit" IMO.

    In fact, K&R compilers supported semi-generic function pointers IIRC. You were
    able to leave out the parameter declaration (but not the return type declaration) e.g.
    int (*foo)();

    So why did the comittee decide against making generic function pointers standard?



  • Nick Bowler

    #2
    Re: Why no generic function pointers?

    On Sat, 21 Jun 2008 02:54:25 +0200, copx wrote:
    Why doesn't the C standard include generic function pointers?
    It does, in a way: you can convert (using a cast) a function pointer to a
    different function pointer type and back again: the result shall compare
    equal to the original pointer. This is similar to the semantics of
    void *, except that the conversion is not implicit.
    This follows the general "generics through void scheme" of C. In fact,
    it seems to be quite "irregular" to me that you can't cast function
    pointers to void.
    On some systems, function pointers are significantly larger than object
    pointers.
    In fact, K&R compilers supported semi-generic function pointers IIRC.
    You were able to leave out the parameter declaration (but not the return
    type declaration) e.g. int (*foo)();
    You can still do this.

    Comment

    • Eric Sosman

      #3
      Re: Why no generic function pointers?

      copx wrote:
      Why doesn't the C standard include generic function pointers?
      >
      I use function pointers a lot and the lack of generic ones is not so cool.
      >
      There is a common compiler extension (supported by GCC and lccwin32 for example)
      which I consider to be perfectly reasonable: you can cast every kind of function pointer
      to a void pointer and void pointers to any kind of function pointer.
      And in strictly-conforming C you can cast any function pointer
      to any other function pointer type and back again. What's your beef?
      This follows the general "generics through void scheme" of C. In fact, it seems to be quite
      "irregular" to me that you can't cast function pointers to void.
      If you're irregular, try a laxative.

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • copx

        #4
        Re: Why no generic function pointers?


        "Nick Bowler" <draconx@gmail. comschrieb im Newsbeitrag news:g3hjqh$csa $1@rumours.uwat erloo.ca...
        On Sat, 21 Jun 2008 02:54:25 +0200, copx wrote:
        >
        >Why doesn't the C standard include generic function pointers?
        >
        It does, in a way: you can convert (using a cast) a function pointer to a
        different function pointer type and back again: the result shall compare
        equal to the original pointer. This is similar to the semantics of
        void *, except that the conversion is not implicit.
        Interesting. Thanks, I didn't know that.




        Comment

        • copx

          #5
          Re: Why no generic function pointers?


          "Eric Sosman" <esosman@ieee-dot-org.invalidschr ieb im Newsbeitrag news:Z_CdnTEqK8 hJxsHVnZ2dnUVZ_ qLinZ2d@comcast .com...
          copx wrote:
          [snip]
          >This follows the general "generics through void scheme" of C. In fact, it seems to be quite
          >"irregular" to me that you can't cast function pointers to void.
          >
          If you're irregular, try a laxative.
          *plonk*


          Comment

          • Keith Thompson

            #6
            Re: Why no generic function pointers?

            "copx" <copx@gazeta.pl writes:
            "Eric Sosman" <esosman@ieee-dot-org.invalidschr ieb im Newsbeitrag news:Z_CdnTEqK8 hJxsHVnZ2dnUVZ_ qLinZ2d@comcast .com...
            >copx wrote:
            [snip]
            >>This follows the general "generics through void scheme" of C. In
            >>fact, it seems to be quite "irregular" to me that you can't cast
            >>function pointers to void.
            >>
            > If you're irregular, try a laxative.
            >
            *plonk*
            Tons of deliberate personal abuse in this newsgroup, and you plonk
            someone over a little joke? I suggest you reconsider -- but if you
            don't, it's your loss.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            • Keith Thompson

              #7
              Re: Why no generic function pointers?

              "copx" <copx@gazeta.pl writes:
              Why doesn't the C standard include generic function pointers?
              [...]

              Remember that there are two distinct ways in which void* is a
              "generic" object pointer.

              One is that a conversion from foo* to void* and back again is
              guaranteed to yield the original value, where foo is any object or
              incomplete type. As others have mentioned, *all* function pointers
              behave this way. If you want a single generic function pointer type,
              I suggest void(*)(void), just because it's the simplest (a typedef
              could be helpful).

              The other way is that conversions between void* and foo* (foo as
              above) may be done implicitly. This isn't the case for function
              pointers, but that's just a matter of syntactic convenience.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Malcolm McLean

                #8
                Re: Why no generic function pointers?


                "copx" <copx@gazeta.pl wrote in message
                Why doesn't the C standard include generic function pointers?
                >
                Because you can't build an argument list on the fly. You can hardcode

                switch(type)
                {
                case 1:
                x = (*fp)(1,2,3);
                break;
                case 2:
                y = (*fp)("My name is fred");
                }

                but you can't pass in a format string and build a list with an arbitrary
                number of integers and strings.

                --
                Free games and programming goodies.


                Comment

                • Ben Bacarisse

                  #9
                  Re: Why no generic function pointers?

                  "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                  "copx" <copx@gazeta.pl wrote in message
                  >Why doesn't the C standard include generic function pointers?
                  >>
                  Because you can't build an argument list on the fly.
                  That begs the question of why one can't do *that*. There are macros
                  to examine an existing var-arg list, and there the could be macros to
                  build one from scratch. Given such a thing, a function pointer type
                  like:

                  void *(*var_arg_fp)( void *, ...);

                  would be very flexible (you need a dummy first argument to get the var
                  arg macros going).

                  I presume the answer is just history. Of course, it may be that there
                  are real systems for which such argument building macros/functions
                  would be impossible, but the seems a stretch.

                  --
                  Ben.

                  Comment

                  • Malcolm McLean

                    #10
                    Re: Why no generic function pointers?

                    "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                    "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                    >
                    >"copx" <copx@gazeta.pl wrote in message
                    >>Why doesn't the C standard include generic function pointers?
                    >>>
                    >Because you can't build an argument list on the fly.
                    >
                    That begs the question of why one can't do *that*. There are macros
                    to examine an existing var-arg list, and there the could be macros to
                    build one from scratch. Given such a thing, a function pointer type
                    like:
                    >
                    void *(*var_arg_fp)( void *, ...);
                    >
                    would be very flexible (you need a dummy first argument to get the var
                    arg macros going).
                    >
                    I presume the answer is just history. Of course, it may be that there
                    are real systems for which such argument building macros/functions
                    would be impossible, but the seems a stretch.
                    >
                    I think that's right.
                    Usually any assembly language routine can be translated to C quite easily.
                    The exception is those assembly routines that build dynamic argument lists.
                    Normally this is relatively strightforwards in assembly - just push the
                    first few arguments into registers and the rest on the stack, but you can't
                    do it in C.

                    It would tend to open security holes, but I don't think that was the
                    historical reason for rejecting the idea. Maybe it was because it doesn't
                    fit well with the structured programming paradigm.

                    --
                    Free games and programming goodies.


                    Comment

                    • Bartc

                      #11
                      Re: Why no generic function pointers?


                      "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                      news:87hcbnt2kz .fsf@bsb.me.uk. ..
                      "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                      >
                      >"copx" <copx@gazeta.pl wrote in message
                      >>Why doesn't the C standard include generic function pointers?
                      >>>
                      >Because you can't build an argument list on the fly.
                      >
                      That begs the question of why one can't do *that*. There are macros
                      to examine an existing var-arg list, and there the could be macros to
                      build one from scratch. Given such a thing, a function pointer type
                      like:
                      >
                      void *(*var_arg_fp)( void *, ...);
                      >
                      would be very flexible (you need a dummy first argument to get the var
                      arg macros going).
                      How would that work? Suppose I have my function pointer fnptr. And I have N
                      (known only at runtime) arguments from an array, say, to pass it. How can I
                      have a loop pushing just the parameters I need?

                      I have an actual need for this; at the moment I'm doing stuff like this
                      (assume fn is a valid function call):

                      switch (N) {
                      case 0: result=fn(); break;
                      case 1: result=fn(param s[0]); break;
                      case 2: result=fn(param s[0],params[1]); break;
                      ...
                      up to some limit.

                      (and the whole thing repeated for different calltypes of fn; and repeated
                      again for different return type of fn: int-sized or double)

                      This works; and is not necessarily slower than a loop, but it's clumsy and
                      does have an arbitrary upper limit on parameters.

                      --
                      Bartc


                      Comment

                      • Malcolm McLean

                        #12
                        Re: Why no generic function pointers?

                        "Bartc" <bc@freeuk.comw rote in message news:
                        >
                        "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                        >>
                        > void *(*var_arg_fp)( void *, ...);
                        >>
                        >would be very flexible (you need a dummy first argument to get the var
                        >arg macros going).
                        >
                        How would that work? Suppose I have my function pointer fnptr. And I have
                        N (known only at runtime) arguments from an array, say, to pass it. How
                        can I have a loop pushing just the parameters I need?
                        >
                        Ben's nodded.

                        What you need is a macro to build a va_arg list

                        va_arg_add(argl ist, type, argument)

                        so user passes a string like "ipippiii" where i means an integer argument
                        and p a char * (to make it simple). We iterate over the string, putting in
                        integers and character pointers, then we call the user-supplied function
                        pointer.

                        To help out the compiler a bit we could specify that the user-defined
                        function must be varidic.

                        --
                        Free games and programming goodies.


                        Comment

                        • Bartc

                          #13
                          Re: Why no generic function pointers?


                          "Malcolm McLean" <regniztar@btin ternet.comwrote in message
                          news:X8OdnSlrzL NqsMDVnZ2dnUVZ8 vSdnZ2d@bt.com. ..
                          "Bartc" <bc@freeuk.comw rote in message news:
                          >>
                          >"Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                          >>>
                          >> void *(*var_arg_fp)( void *, ...);
                          >>>
                          >>would be very flexible (you need a dummy first argument to get the var
                          >>arg macros going).
                          >>
                          >How would that work? Suppose I have my function pointer fnptr. And I have
                          > N (known only at runtime) arguments from an array, say, to pass it.
                          >How
                          >can I have a loop pushing just the parameters I need?
                          >>
                          Ben's nodded.
                          >
                          What you need is a macro to build a va_arg list
                          >
                          va_arg_add(argl ist, type, argument)
                          >
                          so user passes a string like "ipippiii" where i means an integer argument
                          and p a char * (to make it simple). We iterate over the string, putting in
                          integers and character pointers, then we call the user-supplied function
                          pointer.
                          That looks like a compile-time process to me.

                          Just consider the possible implementation of this simplified function
                          interface:

                          int callfunction (void *fnptr, int nparams, int *params);

                          'params' points to a list of int values to be passed.

                          'nparams' is how many ints there are in 'params'.

                          The task is to call fnptr with the parameters as specified, and to return
                          what fnptr returns (I know I haven't fully specified it's signature but
                          assume the return value is int).

                          --
                          Bartc



                          Comment

                          • Malcolm McLean

                            #14
                            Re: Why no generic function pointers?

                            "Bartc" <bc@freeuk.comw rote in message news
                            >
                            "Malcolm McLean" <regniztar@btin ternet.comwrote in message
                            >so user passes a string like "ipippiii" where i means an integer argument
                            >and p a char * (to make it simple). We iterate over the string, putting
                            >in
                            >integers and character pointers, then we call the user-supplied function
                            >pointer.
                            >
                            That looks like a compile-time process to me.
                            >
                            No, the string could be generated anyhow. Imagine this not-too unrealistic
                            scenario.

                            You want the user to type "printf("%d%s\n ", 10, "Fred")"
                            You want the program to call printf() or vprintf() with the arguments he
                            supplied.

                            --
                            Free games and programming goodies.





                            Comment

                            • Tor Rustad

                              #15
                              Re: Why no generic function pointers?

                              copx skrev:
                              Why doesn't the C standard include generic function pointers?
                              >
                              I use function pointers a lot and the lack of generic ones is not so cool.
                              >
                              There is a common compiler extension (supported by GCC and lccwin32 for example)
                              which I consider to be perfectly reasonable: you can cast every kind of function pointer
                              to a void pointer and void pointers to any kind of function pointer.
                              Notice however, that the return type of the POSIX XSI dlsym() function
                              is a void pointer, and in the X/Open System Interface Extension



                              we find:

                              "Implementation s supporting the XSI extension, however, do require that
                              an object of type void * can hold a pointer to a function."


                              In practice, I have yet to run into an implementation where function
                              pointer couldn't be converted to void *, and back. So instead of asking
                              for a generic function pointer, I find the XSI requirement "type void *
                              can hold a pointer to a function" more natural thing to ask for.

                              --
                              Tor <bwzcab@wvtqvm. vw | tr i-za-h a-z>

                              Comment

                              Working...