function pointer question

Discussion in 'C Programming' started by MK, Mar 23, 2006.

  1. MK

    MK Guest

    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)(int,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
     
    MK, Mar 23, 2006
    #1
    1. Advertising

  2. MK

    raxip Guest

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

    If you did, you should get a compiler error complaining about too many
    arguements.
     
    raxip, Mar 23, 2006
    #2
    1. Advertising

  3. MK

    MK Guest

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


    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)(int,int,int);

    int
    main(int argc,char **argv)
    {
    func3args fp = (func3args)func2args;
    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.
     
    MK, Mar 23, 2006
    #3
  4. "MK" <> writes:
    > 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)(int,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)).


    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)f2args;

    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.

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


    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_Keith) <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.
     
    Keith Thompson, Mar 23, 2006
    #4
  5. MK

    MK Guest

    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.
     
    MK, Mar 23, 2006
    #5
  6. MK

    Ian Collins Guest

    MK wrote:
    >
    > 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.
    >

    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.
     
    Ian Collins, Mar 23, 2006
    #6
  7. "MK" <> writes:
    > 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.


    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_Keith) <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.
     
    Keith Thompson, Mar 23, 2006
    #7
  8. "MK" <> wrote in message
    news:...
    > 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.
    >


    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
     
    Rod Pemberton, Mar 23, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    725
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,077
    Fraser Ross
    Aug 14, 2004
  3. Replies:
    10
    Views:
    731
    Chris Torek
    Feb 4, 2005
  4. murgan
    Replies:
    6
    Views:
    4,928
    Thad Smith
    Dec 21, 2005
  5. Vijai Kalyan
    Replies:
    4
    Views:
    739
    Vijai Kalyan
    Nov 8, 2005
Loading...

Share This Page