473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Calling C function in C++ without deafult arguments

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 '06 #1
1 1715
pk*****@gmail.com 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.]
Mar 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shailan | last post by:
Hi Im having trouble with the following code that seems to be behave differently when called from the browser as opposed to the command line. The calling script is a cgi that forks, with the...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
3
by: Vince | last post by:
Hi, I'd like to know how to call a DLL function, when the number of argument can be variant. Thanks, Vince
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
5
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? ...
14
by: ericellsworth | last post by:
Hi, I'm trying to use a class to pass variables back and forth from a form opened in dialog mode. I have created a class which invokes a form in its show method, like so: Public Sub Show() '...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: Spoon | last post by:
Hello everyone, AFAIU, in C89, calling fprintf with too few arguments leads to UB. 4.9.6.1 The fprintf function Synopsis #include <stdio.h> int fprintf(FILE *stream, const char *format,...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.