走私报警处理程序等数据 [英] smuggling data in and out of alarm handlers and the like

查看:87
本文介绍了走私报警处理程序等数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现需要将数据移入或移出函数,而不是直接调用
。例如,报警处理程序或qsort使用的比较

函数。在这些情况下,

不可能将数据作为函数参数传递,因此需要走私

in。

有没有比下面描述的三种方法更好的方法?


1.使用全局变量。这适用于小型单片程序

但是在单独的

文件和/或共享库中使用大量代码的大型程序中并不好。


2.创建一个这样的函数:

SOME_STRUCT * smuggle_pointer(SOME_STRUCT *指针){

静态SOME_STRUCT * hold_pointer;

如果(指针)hold_pointer =指针;

返回(hold_pointer);

}


和在任何可能调用

问题函数之前加载数据。目标函数包含:


SOME_STRUCT *指针;

指针= smuggle_pointer(NULL);


给出功能访问这些数据。


3.使用setenv / getenv。这不是ANSI C的一部分,但通常可用的是
。仅适用于一两个简单的

值。

有更好的方法吗?


谢谢,


David Mathog

解决方案

David Mathog< ma **** @ caltech.eduwrites:


我经常发现需要将数据移入或移出函数,而不是直接调用
。例如,报警处理程序或qsort使用的比较

函数。在这些情况下,没有可能将数据作为函数参数传递,因此需要走私

in。


对于qsort和bsearch,我使用额外的空格

*参数并将其传递给比较函数。 void *

可用于传递任意数据。 (这些函数无法提供任何方式来传递辅助的

数据,实在没有任何借口

。我认为它们的设计很糟糕。)

-

"您的更正是100%正确且0%有帮助。干得好!"

- 理查德希思菲尔德


David Mathog写道:


>

我经常发现需要将数据移入或移出功能,而不是直接调用
。例如,报警处理程序或qsort使用的比较

函数。在这些情况下,没有可能将数据作为函数参数传递,因此需要走私

in。


当你的变量只有少量的价值

时:


例如,

如果你有一个指针数组

到几列的字符串,

你想要qsort字符串

基于一个任意列,

然后你可以定义一个指针数组来表示

不同的比较函数,

你还必须写,

然后变量索引函数指针数组

将成为qsort调用的一部分。


-

pete


David Mathog写道:


Every所以我经常发现需要将数据移入或移出函数,而不是直接调用
。例如,报警处理程序或qsort使用的比较

函数。



这些函数通常称为回调函数。只是为了澄清。


在这些情况下,

不可能将数据作为函数参数传递,因此需要 ;走私

in。



是的,那是一个拙劣的行为。对于qsort()回调,你至少可以传入

指向结构的指针数组,这些结构有一个额外的成员指向

你的走私&数据,这是笨拙的,因为它吃了内存和

性能。


当我开始使用GTK +编程GUI时(围绕回调
$ b) $ b函数),它首先让我觉得奇怪,每一个回调都有一些好奇的void * user_data。看似无用的参数。男孩,是不是因为我在大约10分钟后才意识到这不是b $ b。很奇怪,C(和其他)图书馆的设计师并没有想到这种必要性。


罗伯特


Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.

Is there a better way of doing this than the three methods described below?

1. use global variables. This is fine for small monolithic programs
but isn''t great in larger programs using lots of code in separate
files and/or shared libraries.

2. create a function like this:

SOME_STRUCT *smuggle_pointer(SOME_STRUCT *pointer){
static SOME_STRUCT *hold_pointer;
if(pointer)hold_pointer = pointer;
return(hold_pointer);
}

and load it with data before any possible calls to the
problem functions. The target function contains:

SOME_STRUCT *pointer;
pointer = smuggle_pointer(NULL);

which gives that function access to this data.

3. use setenv/getenv. This is not part of ANSI C, but is
usually available. Only useful for one or two simple
values.
Is there a better way?

Thanks,

David Mathog

解决方案

David Mathog <ma****@caltech.eduwrites:

Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.

For qsort and bsearch, I use replacements that take an extra void
* parameter and pass it to the comparison function. The void *
can be used to pass arbitrary data. (There''s really no excuse
for these functions failing to provide any way to pass auxiliary
data. They are badly designed in my opinion.)
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield


David Mathog wrote:

>
Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.

When there''s only a small number of values
that your variable can have:

For example,
if you had an array of pointers
to strings of a few columns,
and you wanted to qsort the strings
based on an arbitrary column,

then you could define an array of pointers to
different compar functions,
which you would also have to write,
and then a variable indexing the array of function pointers
would be part of the qsort call.

--
pete


David Mathog wrote:

Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort.

Such functions are typically called "callback functions". Just to clarify.

In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.

Yeah, that''s a botch. For the qsort() callback you can at least pass in an
array of pointers to structs that have an additional member which points to
your "smuggled" data, which is clumsy because it eats memory and
performance.

When I started programmming GUIs using GTK+ (which revolves around callback
functions), it at first struck me as odd that each and every callback had
some curious "void *user_data" parameter which seemed useless. Boy, is it
not, as I realized after about 10 minutes. It''s odd that the designers of
the C (and other) libraries didn''t think of this mecessity.

robert


这篇关于走私报警处理程序等数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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