prototype mismatches

Discussion in 'C++' started by Gary, Jan 7, 2004.

  1. Gary

    Gary Guest

    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;
    }
     
    Gary, Jan 7, 2004
    #1
    1. Advertising

  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 (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
     
    Victor Bazarov, Jan 7, 2004
    #2
    1. Advertising

  3. 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'
     
    E. Robert Tisdale, Jan 7, 2004
    #3
  4. Gary

    Gary Guest

    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" <> wrote in message
    news:A2JKb.78971$xX.559130@attbi_s02...
    > "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 (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
    >
    >
     
    Gary, Jan 7, 2004
    #4
  5. Gary

    Gary Guest

    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" <> wrote in message
    news:...
    > 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'
    >
     
    Gary, Jan 7, 2004
    #5
    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. Dave Theese
    Replies:
    1
    Views:
    430
  2. June Lee
    Replies:
    2
    Views:
    843
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    3
    Views:
    197
    Mauricio Fernandez
    Aug 7, 2006
  4. Janus Bor
    Replies:
    3
    Views:
    224
    Harry Kakueki
    Sep 26, 2010
  5. Tim McDaniel

    Detecting sub/call mismatches?

    Tim McDaniel, Nov 21, 2012, in forum: Perl Misc
    Replies:
    3
    Views:
    262
    Rainer Weikusat
    Nov 21, 2012
Loading...

Share This Page