调用args不足的函数(通过函数指针) [英] call function with inadequate args (via function pointer)

查看:98
本文介绍了调用args不足的函数(通过函数指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我可以编译并运行这个代码(见下文)两次调用

函数f,先用太少,第二用太多的争论。


但这是合法的,没有内存泄漏和其他问题吗?当然,

我认为如果通过g调用它我不会访问我。


int f(int i){ / * ... * / return 0; }


int main(int argc,char ** argv){


int(* g)(void)= f; / *少于args而不是f * /

int(* h)(int,int)= f; / *更多args而不是f * /


g();

h(0,1);


返回0;

}


Felix

Hi,

I can compile and run this code (see below) which twice calls the
function f, first with too less, second with too much arguments.

But is it legal and free of memory leaks and other problems? Of course,
I presume that inside f I don''t access i in case it was called via g.

int f(int i){ /* ... */ return 0; }

int main(int argc, char** argv){

int(*g)(void)=f; /* less args than f */
int(*h)(int,int)=f; /* more args than f */

g();
h(0,1);

return 0;
}

Felix

推荐答案

文章< 20 ****************************@googlemail.com>,

Felix Kater< fk *** *@googlemail.comwrote:
In article <20****************************@googlemail.com>,
Felix Kater <fk****@googlemail.comwrote:

>我可以编译并运行这个代码(见下文)两次调用
函数f,先用太少,第二个论点太多了。
>I can compile and run this code (see below) which twice calls the
function f, first with too less, second with too much arguments.


>但它是合法的,没有内存泄漏和其他问题吗?当然,
我认为如果是通过g调用的话,我就不能访问它。
>But is it legal and free of memory leaks and other problems? Of course,
I presume that inside f I don''t access i in case it was called via g.


> int f(int i){/ * ... * / return 0; }
int main(int argc,char ** argv){

int(* g)(void)= f; / *少于args而不是f * /

int(* h)(int,int)= f; / *更多args而不是f * /

g();

h(0,1);

返回0;
}
>int f(int i){ /* ... */ return 0; }
int main(int argc, char** argv){
int(*g)(void)=f; /* less args than f */
int(*h)(int,int)=f; /* more args than f */
g();
h(0,1);
return 0;
}



如果您的呼叫与原型中给出的参数数量不相符,则违反约束条件。如果你的b $ b有一个与参数数量不匹配的电话,那么

不是原型,那就是未定义的行为。


但从某种意义上说,这不是你在做什么,因为你有这些显式转换为不同的函数类型。因此,

管理条款是,


"指向一种类型函数的指针可能会被转换为指针

到另一种类型的功能,然后再回来;结果将等于原始指针的
比较。如果使用转换后的指针

来调用一个类型不兼容的函数,那么行为是未定义的。

参数数量的不匹配不是兼容的。对于

以上的目的。

-

重要的是要记住,在法律方面,计算机

永远不会复制,只有人类才能复制。计算机给出了

命令,而非许可。只有人才能获得许可。

- Brad Templeton

If you have a call that does not match the number of parameters
given in a prototype, it is a constraint violation. If you
have a call that does not match the number of parameters when there
is no prototype, it is undefined behaviour.

But in a sense that''s not what you are doing, in that you have
those explicit casts to a different function type. Therefore the
governing clause is,

"A pointer to a function of one type may be converted to a pointer
to a function of another type and back again; the result shall
compare equal to the original pointer. If a converted pointer
is used to call a function that has a type that is not compatible
with the type of the called function, the behavior is undefined."
A mismatch of the number of parameters is not "compatible" for the
above purposes.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


Felix Kater写道:
Felix Kater wrote:




我可以编译并运行这个代码(见下文)两次调用

函数f,先用太少,第二个有太多的争论。


但它是合法的,没有内存泄漏和其他问题吗?当然,

我认为如果通过g调用它我不会访问我。


int f(int i){ / * ... * / return 0; }


int main(int argc,char ** argv){


int(* g)(void)= f; / *少于args而不是f * /

int(* h)(int,int)= f; / *更多args而不是f * /


g();

h(0,1);


返回0;

}
Hi,

I can compile and run this code (see below) which twice calls the
function f, first with too less, second with too much arguments.

But is it legal and free of memory leaks and other problems? Of course,
I presume that inside f I don''t access i in case it was called via g.

int f(int i){ /* ... */ return 0; }

int main(int argc, char** argv){

int(*g)(void)=f; /* less args than f */
int(*h)(int,int)=f; /* more args than f */

g();
h(0,1);

return 0;
}



这不会导致未定义的行为。指针声明和

函数声明必须匹配。

No this causes undefined behaviour. The pointer declaration and the
function declaration must match.


首先,我要感谢您的详细信息。


2007年1月10日星期三18:40:57 +0000(UTC)
ro ****** @ ibd.nrc-cnrc.gc.ca (Walter Roberson)写道:
First, let me thank You for the details.

On Wed, 10 Jan 2007 18:40:57 +0000 (UTC)
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:

如果你有一个与原型中给出的参数数量不一致的调用,这是违反约束的。如果你的b $ b有一个与参数数量不匹配的电话,那么

不是原型,那就是未定义的行为。


但从某种意义上说,这不是你在做什么,因为你有这些显式转换为不同的函数类型。因此,

管理条款是,


"指向一种类型函数的指针可能会被转换为指针

到另一种类型的功能,然后再回来;结果将等于原始指针的
比较。如果使用转换后的指针

来调用一个类型不兼容的函数,那么行为是未定义的。


参数数量的不匹配不是兼容的。对于

以上的目的。
If you have a call that does not match the number of parameters
given in a prototype, it is a constraint violation. If you
have a call that does not match the number of parameters when there
is no prototype, it is undefined behaviour.

But in a sense that''s not what you are doing, in that you have
those explicit casts to a different function type. Therefore the
governing clause is,

"A pointer to a function of one type may be converted to a pointer
to a function of another type and back again; the result shall
compare equal to the original pointer. If a converted pointer
is used to call a function that has a type that is not compatible
with the type of the called function, the behavior is undefined."
A mismatch of the number of parameters is not "compatible" for the
above purposes.



所以,我想稍微修改一下这个例子:如果使用像这些中的变量参数那么

会是什么案例(a + b):


(a)


/ *在这里添加var args:* /

int f(int i,...){/ * ... * / return 0; }


int main(int argc,char ** argv){


int(* g)(int,int)= f;

int(* h)(int,int,int)= f;


g(0,1);

h( 0,1,2);


返回0;

}

(b)


int f1(int i){/ * ... * / return 0; }

int f2(int i,int k){/ * ... * / return 0; }


int main(int argc,char ** argv){


/ *仅一个函数指针...:* /

int(* g)(int,...);


/ * ...在第一个上下文中使用:* /

g = f1;

g(0); / *确切的参数数量? * /

g(0,1); /* 争论太多? * /


/ * ...在其他情况下使用:* /

g = f2;

g(0); / *没有足够的论据? * /

g(0,1,2); /* 争论太多? * /


返回0;

}


案例(a)可能与是相匹配相容QUOT;在你的

解释中因为直接调用带有可变参数的f是合法的




然而,是什么情况下的情况(b)?


Felix

So, I''d like to modify the example a bit: What would be in case of
using variable arguments like in these to cases (a + b):

(a)

/* added var args here: */
int f(int i,...){ /* ... */ return 0; }

int main(int argc, char** argv){

int(*g)(int,int)=f;
int(*h)(int,int,int)=f;

g(0,1);
h(0,1,2);

return 0;
}
(b)

int f1(int i){ /* ... */ return 0; }
int f2(int i, int k){ /* ... */ return 0; }

int main(int argc, char** argv){

/* one function pointer only...: */
int(*g)(int,...);

/* ...used in the first context: */
g=f1;
g(0); /* exact number of arguments? */
g(0,1); /* too many arguments? */

/* ...used in another context: */
g=f2;
g(0); /* not enough arguments? */
g(0,1,2); /* too many arguments? */

return 0;
}

Case (a) probably matches what is ment by "compatible" in your
explanation since direct calls to f with variable arguments are legal
as well.

However, what is with case (b)?

Felix


这篇关于调用args不足的函数(通过函数指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆