473,698 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

prototype mismatches

I recently joined a project with a mix of K&R C and ANSI C++. There are
instances
of prototype mismatches defined in one file and used in another that aren't
being caught and result in run time errors (see example below).

Is there a utility that can catch such mismatches (e.g. lint or something)?
I'm constrained in that I'm not allowed to make massive code changes.
Thanks in advance,
Gary

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}

/********** file2.C ***************/
/* programmer introduces bug by declaring prototype
with one arg instead of two. */
extern "C" int add(int x);

int main(int argc, char* argv[])
{
int z = add(3); /* <-- incorrect signature not caught by linker */
return 0;
}


Jul 22 '05 #1
4 1583
"Gary" <gf***@thoughtv ector.com> wrote...
I recently joined a project with a mix of K&R C and ANSI C++. There are
instances
of prototype mismatches defined in one file and used in another that aren't being caught and result in run time errors (see example below).

Is there a utility that can catch such mismatches (e.g. lint or something)? I'm constrained in that I'm not allowed to make massive code changes.
Thanks in advance,
Gary

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}

/********** file2.C ***************/
/* programmer introduces bug by declaring prototype
with one arg instead of two. */
extern "C" int add(int x);

int main(int argc, char* argv[])
{
int z = add(3); /* <-- incorrect signature not caught by linker */
return 0;
}


First of all, the C++ program (the only relevant part of your post
here in comp.lang.c++) is _valid_. The fact that 'int add(int)' is
not defined should be caught by the linker and reported as error,
but there is no language problem since the prototype matches the use.

Second, the utility you're looking for is called "C++ compiler".
Take your K&R C code and copy into files with extension '.C' and run
them through the C++ compiler. Correct reported errors. Repeat
until no errors reported. Stuff the K&R code into an archive and
deposit it into a safe to never be seen again.

Victor
Jul 22 '05 #2
Gary wrote:
I recently joined a project with a mix of K&R C and ANSI C++.
There are instances of prototype mismatches
defined in one file and used in another
that aren't being caught and result in run time errors.

Is there a utility that can catch such mismatches (e.g. lint or something)?
I'm constrained in that I'm not allowed to make massive code changes.

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
} cat file1.h /********** file1.h ***************/
/* declares a function with two args */
#ifndef GUARD_FILE1_H
#define GUARD_FILE1_H 1
#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
extern int add(int, int);
#ifdef __cplusplus
}
#endif/*__cplusplus */
#endif/*GUARD_FILE1_H 1 */
cat file1.1.c #include "file1.h"
#include "file1.c"
gcc -Wall -std=c99 -pedantic -o file1.o -c file1.1.c
cat file2.C /********** file2.C ***************/
/* programmer fixes bug
* by including header file. */

#include "file1.h"

int main(int argc, char* argv[]) {
int z = add(3);/* <-- incorrect signature caught by compiler*/
return 0;
}
g++ -Wall -ansi -pedantic -o file2 file2.C file1.o

file1.h: In function `int main(int, char**)':
file1.h:8: too few arguments to function `int add(int, int)'
file2.C:8: at this point in file
file2.C:8: warning: unused variable `int z'

Jul 22 '05 #3
Your sarcasm aside, you apparently missed reading that one of my constraints
is that my project manager won't permit large scale code changes. This is
legacy production code.

Gary

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:A2JKb.7897 1$xX.559130@att bi_s02...
"Gary" <gf***@thoughtv ector.com> wrote...
I recently joined a project with a mix of K&R C and ANSI C++. There are
instances
of prototype mismatches defined in one file and used in another that

aren't
being caught and result in run time errors (see example below).

Is there a utility that can catch such mismatches (e.g. lint or

something)?
I'm constrained in that I'm not allowed to make massive code changes.
Thanks in advance,
Gary

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}

/********** file2.C ***************/
/* programmer introduces bug by declaring prototype
with one arg instead of two. */
extern "C" int add(int x);

int main(int argc, char* argv[])
{
int z = add(3); /* <-- incorrect signature not caught by linker */
return 0;
}


First of all, the C++ program (the only relevant part of your post
here in comp.lang.c++) is _valid_. The fact that 'int add(int)' is
not defined should be caught by the linker and reported as error,
but there is no language problem since the prototype matches the use.

Second, the utility you're looking for is called "C++ compiler".
Take your K&R C code and copy into files with extension '.C' and run
them through the C++ compiler. Correct reported errors. Repeat
until no errors reported. Stuff the K&R code into an archive and
deposit it into a safe to never be seen again.

Victor

Jul 22 '05 #4
Thanks for the great suggestion. While I can't implement it directly since
I'm not allowed to change lots of production source code, it gave me other
ideas about autogenerating an alternate build directory.
Gary
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:3F******** ******@jpl.nasa .gov...
Gary wrote:
I recently joined a project with a mix of K&R C and ANSI C++.
There are instances of prototype mismatches
defined in one file and used in another
that aren't being caught and result in run time errors.

Is there a utility that can catch such mismatches (e.g. lint or something)? I'm constrained in that I'm not allowed to make massive code changes.

/********** file1.c ***************/
/* defines a function with two args */
extern int add();

int add(x, y)
int x;
int y;
{
return x+y;
}

> cat file1.h

/********** file1.h ***************/
/* declares a function with two args */
#ifndef GUARD_FILE1_H
#define GUARD_FILE1_H 1
#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
extern int add(int, int);
#ifdef __cplusplus
}
#endif/*__cplusplus */
#endif/*GUARD_FILE1_H 1 */
> cat file1.1.c

#include "file1.h"
#include "file1.c"
> gcc -Wall -std=c99 -pedantic -o file1.o -c file1.1.c
> cat file2.C

/********** file2.C ***************/
/* programmer fixes bug
* by including header file. */

#include "file1.h"

int main(int argc, char* argv[]) {
int z = add(3);/* <-- incorrect signature caught by compiler*/
return 0;
}
> g++ -Wall -ansi -pedantic -o file2 file2.C file1.o

file1.h: In function `int main(int, char**)':
file1.h:8: too few arguments to function `int add(int, int)'
file2.C:8: at this point in file
file2.C:8: warning: unused variable `int z'

Jul 22 '05 #5

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

Similar topics

8
3751
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the bind function. The problem with Javascript is that the "this" operator is poorly overloaded and it is often hard to understand in the context of object-oriented javascript So, let's start with the definition:
8
2058
by: Robert | last post by:
Hi, I can use "with" like this: function MyObject(message) { this.message = message; } function _MyObject_speak() {
2
3029
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass' priviledged methods also. Do you know if this is possible ? In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method...
4
1765
by: lkrubner | last post by:
I'm reading an essay, I think one of Crockford's, and it has this example in it: function Demo() { } Demo.prototype = new Ancestor(); Demo.prototype.foo = function () { } ; Does Ancestor now have a function called foo? What if I have 5 different objects, all descended from Ancestor? Do
21
3845
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
5
2237
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
11
1886
by: BD | last post by:
Hi, all. I'm running 8.2 on Windows. This is a development platform for a project whose production environment is running on a mainframe. I believe that the RI compilation process is not quite as robust on Windows as it is in other environments. Here's what I'm finding:
4
1898
by: vshenoy | last post by:
Hi Guys, I was going through gdbm-1.8.3 source (http://ftp.gnu.org/gnu/gdbm/ gdbm-1.8.3.tar.gz) and found this strange thing : all the exposed functions of gdbm work with GDBM_FILE pointer (which is returned by gdbm_open), but in the implementation of gdbm functions (e.g. gdbm_open in gdbmopen.c)work with a structure called gdbm_file_info. Now GDBM_FILE is defined like this in gdbm.h an exposed header :
2
1824
by: marybrown | last post by:
i will write the complete problem i am facing. Here is the input file i am using. sxoght: #query hit score probability qstart qend qorientation tstart tend matches mismatches gapOpening gaps @SNPSTER4_104_308EFAAXX:1:1:1694:128 GGGATAAGAGAGGTGCATGTTGGTATTTAAGGTAGT 1 alignment(s) -- reports limited to 10 alignment(s) sxoght: SNPSTER4_104_308EFAAXX:1:1:1694:128 gi|122939163|ref|NM_000165.3| -10 ...
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7732
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.