本文介绍了理解在C和C ++ F()和f(空隙)之间​​的差值一劳永逸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ok, so I have heard different opinions on this subject and just want to make sure I understand it correctly.

For C++

Declarations void f(); and void f(void); mean precisely the same thing, the function f does not take any parameters. Ditto for definitions.

For C

Declaration void f(void); means that f does not take any parameters.

Declaration void f(); means that function f may or may not have parameters, and if it does, we don't know what kind of parameters those are, or how many there is of them. Note that it is NOT the same as ellipsis, we can't use va_list.

Now here is where things get interesting.

Case 1

Declaration:

void f();

Definition:

void f(int a, int b, float c)
{
   //...
}

Case 2

Declaration:

void f();

Definition:

void f()
{
   //...
}

Question:

What happens at compile time in cases 1 and 2 when we call f with the correct arguments, wrong arguments and no arguments at all? What happens at run time?

Additional question:

If I declare f with arguments, but define it without them, will it make a difference? Should I be able to address the arguments from the function body?

解决方案

More terminology (C, not C++): a prototype for a function declares the types of its arguments. Otherwise the function does not have a prototype.

void f();                      // Declaration, but not a prototype
void f(void);                  // Declaration and prototype
void f(int a, int b, float c); // Declaration and prototype

Declarations that aren't prototypes are holdovers from pre-ANSI C, from the days of K&R C. The only reason to use an old-style declaration is to maintain binary compatibility with old code. For example, in Gtk 2 there is a function declaration without a prototype -- it is there by accident, but it can't be removed without breaking binaries. The C99 standard comments:

Recommendation: I suggest compiling all C code in GCC/Clang with -Wstrict-prototypes and -Wmissing-prototypes, in addition to the usual -Wall -Wextra.

What happens

void f(); // declaration
void f(int a, int b, float c) { } // ERROR

The declaration disagrees with the function body! This is actually a compile time error, and it's because you can't have a float argument in a function without a prototype. The reason you can't use a float in an unprototyped function is because when you call such a function, all of the arguments get promoted using certain default promotions. Here's a fixed example:

void f();

void g()
{
    char a;
    int b;
    float c;
    f(a, b, c);
}

In this program, a is promoted to int and c is promoted to double. So the definition for f() has to be:

void f(int a, int b, double c)
{
    ...
}

See C99 6.7.6 paragraph 15,

Answer 1

When you call f(), the parameters get promoted using the default promotions. If the promoted types match the actual parameter types for f(), then all is good. If they don't match, it will probably compile but you will definitely get undefined behavior.

"Undefined behavior" is spec-speak for "we make no guarantees about what will happen." Maybe your program will crash, maybe it will work fine, maybe it will invite your in-laws over for dinner.

There are two ways to get diagnostics at compile-time. If you have a sophisticated compiler with cross-module static analysis capabilities, then you will probably get an error message. You can also get messages for un-prototyped function declarations with GCC, using -Wstrict-prototypes -- which I recommend turning on in all your projects (except for files which use Gtk 2).

Answer 2

It shouldn't compile.

Exceptions

There are actually two cases in which function arguments are allowed to disagree with the function definition.

  1. It is okay to pass char * to a function that expects void *, and vice versa.

  2. It is okay to pass a signed integer type to a function that expects the unsigned version of that type, or vice versa, as long as the value is representable in both types (i.e., it is not negative, and not out of range of the signed type).

Footnotes

: It is possible that char promotes to unsigned int, but this is very uncommon.

这篇关于理解在C和C ++ F()和f(空隙)之间​​的差值一劳永逸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 13:22