Calling C function in C++ without deafult arguments

Discussion in 'C++' started by pkpatil@gmail.com, Mar 4, 2006.

  1. Guest

    Hi,

    I recently saw a code with a mix of C & C++, wherein a C++ file was
    calling a C function dropping the last argument. The code was similar
    to below:
    --------------------------------------------------------------------------------------------------------------
    cfile.c
    ---------
    C_Fun(int arg1, int arg2, int arg3) { // C function with three
    parameters
    }

    cppfile.cpp
    ----------------
    extern "C" { C_Fun(int arg1, int arg2); } //Note: declaring the C
    function as taking only two parameters

    OtherFunction() {
    int first, int second;
    C_Fun(first, second) //Note: passing only two parameters
    }
    --------------------------------------------------------------------------------------------------------------

    This code did get built when linked together (using gcc), but how is
    this allowed? Is the last argument to C function, "arg3" treated as
    default argument with value of zero as
    "C_Fun(int arg1, int arg2, int arg3=0);" ?

    Thanks,
    PK
    , Mar 4, 2006
    #1
    1. Advertising

  2. Artie Gold Guest

    wrote:
    > Hi,
    >
    > I recently saw a code with a mix of C & C++, wherein a C++ file was
    > calling a C function dropping the last argument. The code was similar
    > to below:
    > --------------------------------------------------------------------------------------------------------------
    > cfile.c
    > ---------
    > C_Fun(int arg1, int arg2, int arg3) { // C function with three
    > parameters
    > }
    >
    > cppfile.cpp
    > ----------------
    > extern "C" { C_Fun(int arg1, int arg2); } //Note: declaring the C
    > function as taking only two parameters
    >
    > OtherFunction() {
    > int first, int second;
    > C_Fun(first, second) //Note: passing only two parameters
    > }
    > --------------------------------------------------------------------------------------------------------------
    >
    > This code did get built when linked together (using gcc), but how is
    > this allowed? Is the last argument to C function, "arg3" treated as
    > default argument with value of zero as
    > "C_Fun(int arg1, int arg2, int arg3=0);" ?
    >
    > Thanks,
    > PK
    >


    It's undefined behavior. Whoever wrote the code lied. From a language
    perspective, anything, including nasal demons[1] may ensue.

    A further perspective is that C++ linkage encodes the number and types
    of parameters in the mangled name that the linker sees, and is hence
    more type safe; C (and hence "C" linkage in C++) does no such thing --
    allowing one to (as in this case) shoot oneself in the foot. Or worse.

    HTH,
    --ag

    [1] Google for it.

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    "You can't KISS* unless you MISS**"
    [*-Keep it simple, stupid. **-Make it simple, stupid.]
    Artie Gold, Mar 4, 2006
    #2
    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. gerlar2000
    Replies:
    0
    Views:
    617
    gerlar2000
    Feb 21, 2005
  2. w.p.
    Replies:
    1
    Views:
    374
    Mudcat
    Jun 6, 2005
  3. Ira
    Replies:
    7
    Views:
    304
    Michael Hudson
    Aug 1, 2005
  4. Garg
    Replies:
    2
    Views:
    1,224
    Oliver Wong
    Apr 16, 2007
  5. Navaneeth
    Replies:
    4
    Views:
    550
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page