Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling C function in C++ without deafult arguments

Reply
Thread Tools

Calling C function in C++ without deafult arguments

 
 
pkpatil@gmail.com
Guest
Posts: n/a
 
      03-04-2006
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

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      03-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
deafult password of tomcat manager Garg Java 2 04-16-2007 02:54 PM
Changing interpreter's deafult output/error streams Ira Python 7 08-01-2005 12:57 PM
[wxPython] How to change deafult tab traversing (radiobuttons & panel) w.p. Python 1 06-06-2005 10:57 PM
Generating pdf with iso-8859-1 using fop on Linux with deafult encoding utf-8 gerlar2000 Java 0 02-21-2005 09:24 PM



Advertisments