Thread: Checking function arguments

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    Question Checking function arguments

    Hi,

    I am new to programming in C, but (as a scientist) have a lot of programming experience in Fortran 95.

    I am trying to figure out how to get the compiler to raise an error if a function is incorrectly called. For example, the following file compiles without a problem with both gcc and icc:

    Code:
    void test(int a) {}
    
    int main(void) {
      float x;
      x = 1.;
      test(x);
      return 0;
    }
    Yet there is clearly a mistake, as test() should be called with an integer, not a float. Are there any debugging flags that will allow incorrect calls like this to be identified?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Some compilers MAY warn about it. g++ (gcc's C++) compiler WILL warn about it if you give "-Wall" as the warning level. Unless you have bits of code that is directly invalid C++ code, you can always compile C code with a C++ compiler just to catch errors.

    There are tools that report this sort of error: lint and coverity are two that I can think of - whether either will give you any error for THIS particular problem, I don't know.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    It appears you are intentionally failing this? I would call some assert() prior to the test() if you need to debug prior to the call.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Quote Originally Posted by slingerland3g View Post
    It appears you are intentionally failing this? I would call some assert() prior to the test() if you need to debug prior to the call.
    This is just to demonstrate what I mean, but in practice I will be writing a program that is very large in which I might accidently call functions with the wrong type of arguments, so need a way to identify such mis-calls.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Quote Originally Posted by matsp View Post
    Some compilers MAY warn about it. g++ (gcc's C++) compiler WILL warn about it if you give "-Wall" as the warning level. Unless you have bits of code that is directly invalid C++ code, you can always compile C code with a C++ compiler just to catch errors.

    There are tools that report this sort of error: lint and coverity are two that I can think of - whether either will give you any error for THIS particular problem, I don't know.

    --
    Mats
    I just tried with g++ and it did not produce any error:

    Code:
    $ g++ --version
    i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
    $ g++ -Wall main.c 
    $ ./a.out

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I may be going down the wrong direction here. But have you thought about creating pointers to functions? This will allow the possibility of you to call the correct function based on your type of arguments. That way the correct function call can be used in such cases of many different argument types. A cool way of overloading function calls within c but can get cryptic when trying to self document code. So good headers of your functions will be needed.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Interesting, gcc-mingw 3.4.x gives a warning in g++, but not in gcc - I just tried it on my machine a few minutes ago.

    Function pointers won't help - you still need to pass the right (type of) arguments to functions.

    PC-lint runs on Windows and is pretty good - but Flexelint is available for MacOS, and according to the manufacturers site, error 747 should be shown for this
    http://gimpel-online.com/MsgRef.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    What the ..........? Did you all forget about function prototypes?
    Code:
    void test(int);
    http://en.wikipedia.org/wiki/Function_prototype
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since the function is defined before main, the presence/absence of a prototype is irrelevant in this case.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    That makes no sence, but thanks for the info.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why doesn't it make sense? The function prototype is there to stand in place of the actual function itself for checking purposes. If you have the function itself, then you don't need the prototype to stand in for it, since it is, in fact, already there.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by melkor445 View Post
    This is just to demonstrate what I mean, but in practice I will be writing a program that is very large in which I might accidently call functions with the wrong type of arguments, so need a way to identify such mis-calls.
    It's not a "miscall." In C, implicit conversions between numeric types are allowed. Some compilers might warn, but many may not. Passing a float to a function expecting an int will work, provided the function is properly prototyped. That's just the way the language is.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    The reason it hasn't produced an error is because C can implicitly convert float type to an integer type and vice versa. So that program is for all intents and purposes correct, so be careful as I know in your line of work the numerical data precision error this can cause can lose you a space vehicle . It's been a long time since I've done C (almost 2 years) but I'll find a way in a few hours to solve ya problem. Soon as I find my C Brain. I think.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    There's no error with the implicit cast that way...

    It's only if there was an implicit cast from float -> int you'd have a problem.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hauzer View Post
    What the ..........? Did you all forget about function prototypes?
    Code:
    void test(int);
    http://en.wikipedia.org/wiki/Function_prototype
    Code:
    void test(int a);
    http://cpwiki.sf.net/Don't_remove_parameter_names
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM