function pointer question

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

    #1

    function pointer question

    Hello,
    I have been trying to get a handle on the following piece of code.
    ----- BEGIN ----
    #include <stdio.h>

    int
    f2args(int a,int b)
    {
    printf("a=%d,b= %d\n",a,b);
    return 1;
    }

    typedef int (*func3args)(in t,int,int);

    int
    main(int argc,char **argv)
    {
    func3args fp = (func3args)f;
    fp(1,2,3);
    exit(0);
    }
    ----- END ----

    weird things:
    - I get no compiler warnings or errors (porbably because I used a cast
    (func)).
    - And when I run I expect the program to crash, but it runs fine.

    $ ./a.out
    a=1,b=2

    My questions are:
    - Is this portable and guranteed by the C standard?
    - Or is it the manifestation of the C calling convention (the
    default), hence not portable or
    implementation dependent.
    I dug aroung and saw a lot of information regarding 'function pointer
    compatibility', I guess I
    don't fully understand that.

    Thanks
    MK

  • raxip

    #2
    Re: function pointer question

    I don't even see how that program would compile. Do you mean
    'func3args fp = (func3args)f2ar gs'?

    If you did, you should get a compiler error complaining about too many
    arguements.

    Comment

    • MK

      #3
      Re: function pointer question

      raxip wrote:[color=blue]
      > I don't even see how that program would compile. Do you mean
      > 'func3args fp = (func3args)f2ar gs'?
      >
      > If you did, you should get a compiler error complaining about too many
      > arguements.[/color]

      raxip:

      I am sorry something went awry with the cut and paste yes it was, here
      is the whole
      program and compile and run log (Linux x86, gcc 3.3.5).
      --- Begin xx.c ----
      $ cat xx.c
      #include <stdio.h>

      int
      func2args(int a,int b)
      {
      printf("a=%d,b= %d\n",a,b);
      return 1;
      }

      typedef int (*func3args)(in t,int,int);

      int
      main(int argc,char **argv)
      {
      func3args fp = (func3args)func 2args;
      fp(1,2,3);
      exit(0);
      }
      ------------------ End xx.c -------
      $ cc xx.c

      $ ./a.out
      a=1,b=2

      --------------------------
      Now, I understand that casting func2args probably is 'shutting' the
      compiler up. As I believe it should be legal to do that.

      Comment

      • Keith Thompson

        #4
        Re: function pointer question

        "MK" <Wavy2Gravy@gma il.com> writes:[color=blue]
        > Hello,
        > I have been trying to get a handle on the following piece of code.
        > ----- BEGIN ----
        > #include <stdio.h>
        >
        > int
        > f2args(int a,int b)
        > {
        > printf("a=%d,b= %d\n",a,b);
        > return 1;
        > }
        >
        > typedef int (*func3args)(in t,int,int);
        >
        > int
        > main(int argc,char **argv)
        > {
        > func3args fp = (func3args)f;
        > fp(1,2,3);
        > exit(0);
        > }
        > ----- END ----
        >
        > weird things:
        > - I get no compiler warnings or errors (porbably because I used a cast
        > (func)).[/color]

        When I compiled it, I got:

        tmp.c: In function `main':
        tmp.c:15: error: `f' undeclared (first use in this function)
        tmp.c:15: error: (Each undeclared identifier is reported only once
        tmp.c:15: error: for each function it appears in.)

        I presume your line
        func3args fp = (func3args)f;
        should be
        func3args fp = (func3args)f2ar gs;

        If you're going to post code, it's very important to post the *exact*
        code that you compiled. Copy-and-paste it, don't re-type it.
        [color=blue]
        > - And when I run I expect the program to crash, but it runs fine.[/color]
        [color=blue]
        > $ ./a.out
        > a=1,b=2
        >
        > My questions are:
        > - Is this portable and guranteed by the C standard?
        > - Or is it the manifestation of the C calling convention (the
        > default), hence not portable or
        > implementation dependent.
        > I dug aroung and saw a lot of information regarding 'function pointer
        > compatibility', I guess I
        > don't fully understand that.[/color]

        The conversion is ok, but the call invokes undefined behavior.

        C99 6.3.2.3p8:

        A pointer to a function of one type may be converted to a pointer
        to a function of another type and back again; the result shall
        compare equal to the original pointer. If a converted pointer is
        used to call a function whose type is not compatible with the
        pointed-to type, the behavior is undefined.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • MK

          #5
          Re: function pointer question

          Keith,
          Thanks for the reply. I did repost the correct code in my reply to
          'raxip' post. I apologize. I see from your post that the behavior is
          undefined. So what defines the functions 'type'? Is it the just
          the 'return type' or the whole signature (including the
          parameters?).
          The reason I am interested in this, I am looking at a large
          code-base, and this 'magic' seems to have been used a lot.
          Thanks.

          Comment

          • Ian Collins

            #6
            Re: function pointer question

            MK wrote:[color=blue]
            >
            > My questions are:
            > - Is this portable and guranteed by the C standard?
            > - Or is it the manifestation of the C calling convention (the
            > default), hence not portable or
            > implementation dependent.
            > I dug aroung and saw a lot of information regarding 'function pointer
            > compatibility', I guess I
            > don't fully understand that.
            >[/color]
            Based on your second post, I'd say it's undefined behaviour which just
            happens to wok. Correct me if I'm wrong (I silly thing to say here!),
            but I don't think there is a standard calling convention.

            A classic case of an evil cast.

            --
            Ian Collins.

            Comment

            • Keith Thompson

              #7
              Re: function pointer question

              "MK" <Wavy2Gravy@gma il.com> writes:[color=blue]
              > Keith,
              > Thanks for the reply. I did repost the correct code in my reply to
              > 'raxip' post. I apologize. I see from your post that the behavior is
              > undefined. So what defines the functions 'type'? Is it the just
              > the 'return type' or the whole signature (including the
              > parameters?).
              > The reason I am interested in this, I am looking at a large
              > code-base, and this 'magic' seems to have been used a lot.[/color]

              The type of a function is determined by the return type and
              parameters.

              It's possible that your system's calling convention (something not
              determined by the C language) is such that this kind of thing happens
              to work. You *might* be better off leaving the code as it is,
              undefined behavior and all, than fixing it at the risk of introducing
              new bugs. You'll have to make that judgement yourself.

              And please read <http://cfaj.freeshell. org/google/>.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Rod Pemberton

                #8
                Re: function pointer question


                "MK" <Wavy2Gravy@gma il.com> wrote in message
                news:1143085691 .556017.280620@ u72g2000cwu.goo glegroups.com.. .[color=blue]
                > Keith,
                > Thanks for the reply. I did repost the correct code in my reply to
                > 'raxip' post. I apologize. I see from your post that the behavior is
                > undefined. So what defines the functions 'type'? Is it the just
                > the 'return type' or the whole signature (including the
                > parameters?).
                > The reason I am interested in this, I am looking at a large
                > code-base, and this 'magic' seems to have been used a lot.
                > Thanks.
                >[/color]

                Those casts make me think of a few things:

                1) A couple of functions with a different number of arguments were merged
                together
                2) The underlying functions are written in another language, i.e., C
                language with Pascal OS calls

                The second is common on a number of miniframes.


                Rod Pemberton


                Comment

                Working...