如何让GCC警告无与伦比的数量的参数使得函数调用时? [英] How to let GCC warn unmatched number of arguments when making function calls?
问题描述
我刚刚调试一个C程序很长一段时间,才发现我做一个函数调用时错过了一个说法,所以垃圾,而不是填补缺少的参数。这样愚蠢的错误是很无奈,但我想编制者应能够检测到这一点。 (C甚至不支持默认参数,即使在C ++中,默认参数需要显式声明)
更新:原型被认为是错误的,太...
那么,是有无与伦比的警告函数调用的参数数量的GCC标志?我一直有 -Wall
和 -pedantic
上;这是相当令人惊讶,这样一个明显的错误未被发现。 (其实我想有一些原因,GCC不报告,但我不能在这个时候想到的任何。)
尴尬code例如:
静态无效DFS(); INT主(INT ARGC,为const char * argv的[]){
DFS(1);
} 静态无效
DFS(INT依然存在,去年INT){
// DFS
}
我只是做了另一项发现是,如果原型包含参数,编译器将报告;但原型碰巧包含任何参数,那么编译器只是下滑。
函数调用参数不匹配的号码是强制性的诊断,所有的编译器都必须提供没有任何特殊的设置。它是由标准规定的。
C99Standard 6.5.2.2函数调用:结果
限制
如果它表示所调用的函数的前pression有一个类型,其中包括一个原型,该
参数的数目应的参数的数量一致。每个参数应
有一种类型,使得它的值可以被分配给一个对象与不合格版本
的其对应的参数的类型。
块引用>静态无效DFS();
告诉编译器
DFS
是静态
函数返回一个无效
,可以采取的参数数目不详。此外,您提供哪些需要两个参数和放大器功能的定义;调用相同。正如你看到的,没有合同的断裂。问题是函数的声明不正确。如果要声明一个函数,它不带任何参数,你必须使用:静态无效DFS(无效);
一旦你这样做的 编译器将为您提供一个诊断 。
I just debugged a C program for a long time, only to find that I missed an argument when making a function call, so junk instead filled the missing argument. Stupid mistakes like this are really frustrating, but I suppose compilers should be able to detect this. (C doesn't even support default arguments; even in C++, default arguments need to be explicitly declared.)
Update: The prototype was found to be wrong, too...
So, is there a GCC flag for warning unmatched function call argument number? I always have
-Wall
and-pedantic
on; it's quite surprising that such an obvious error goes undetected. (Actually I suppose there is some reason that GCC does not report, but I can't think of any at this time.)Embarrassing code example:
static void dfs(); int main(int argc, const char *argv[]) { dfs(1); } static void dfs(int remain, int last) { // dfs }
Another discovery I just made is that if the prototype contains argument, the compiler will report; but the prototype happened to contains no arguments, then the compiler just slipped.
解决方案Unmatched number of function call arguments is a mandatory diagnostic that all compilers will and must provide without any special setting. It is mandated by the standard.
C99Standard 6.5.2.2 Function calls:
ConstraintsIf the expression that denotes the called function has a type that includes a prototype, the number of arguments shall agree with the number of parameters. Each argument shall have a type such that its value may be assigned to an object with the unqualified version of the type of its corresponding parameter.
static void dfs();
Tells the compiler
dfs
is astatic
function which returns avoid
and can take unspecified number of arguments. Further you provide a definition for the function which takes 2 arguments & call the same. As you see there is no breaking of contract. The problem is the declaration of the function is incorrect. If you want to declare a function which takes no arguments you must use:static void dfs(void);
Once you do that the compiler will provide you a diagnostic.
这篇关于如何让GCC警告无与伦比的数量的参数使得函数调用时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!