从C函数返回多个值 [英] Returning multiple values from a C function

查看:129
本文介绍了从C函数返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重要提示:请参阅本非常相关的问题:在C ++中返回多个值。

Important: Please see this very much related question: Return multiple values in C++.

我以后该怎么做同样的事情在ANSI C是?你会使用一个结构或传递则params的地址的功能?我后非常有效的(快)code(时间和空间)来的,甚至在可读性成本。

I'm after how to do the same thing in ANSI C? Would you use a struct or pass the addresses of the params in the function? I'm after extremely efficient (fast) code (time and space), even at the cost of readability.

修改:感谢所有的答案。好吧,我觉得我欠了一些解释:我在写这本书的算法特定域的某个子集。我已经给自己设定了最有效地(时间和空间)的实现我所有的交易算法忍受在网络上,在可读性和其他东西的成本很随意的目标。这部分是我的(普通)问题的性质。

EDIT: Thanks for all the answers. Ok, I think I owe some explanation: I'm writing this book about a certain subset of algorithms for a particular domain. I have set myself the quite arbitrary goal of making the most efficient (time and space) implementations for all my algos to put up on the web, at the cost of readability and other stuff. That is in part the nature of my (general) question.

:我希望我得到这个直,从(可能)最快多见无意义的(这一切是先验的,即没有测试):

Answer: I hope I get this straight, from (possibly) fastest to more common-sensical (all of this a priori, i.e. without testing):


  1. 在全局对象存储outvalues​​(我假设像outvals [2]),或

  2. 传递outvalues​​在函数PARAMS(美孚(INT中,诠释* OUT1,为int * OUT2)),或

  3. 返回一个结构既outvals,或

  4. (3)只有当值语义相关的。

这是否有道理?如果是这样,我认为贾森的反应是最接近的,尽管它们都提供了一些一块拼图的。罗伯特的是好的,但在这个时候语义是不是我以后我(虽然他的建议是适当的注意)

Does this make sense? If so, I think Jason's response is the closest, even though they all provide some piece of the "puzzle". Robert's is fine, but at this time semantics is not what I'm after (although his advice is duly noted)

感谢大家。

推荐答案

随着尼尔说,你需要判断它自己。

As Neil says, you need to judge it for yourself.

要避免将任何东西的成本,使用全局。接下来最好是指针/引用传递的单一结构。在此之后有个别指针/引用PARAMS。

To avoid the cost of passing anything, use a global. Next best is a single structure passed by pointer/reference. After that are individual pointer/reference params.

不过,如果你将数据打包成结构,然后读背出电话后,你可能会更好传递各个参数。

However, if you have to pack data into the structure and then read it back out after the call, you may be better off passing individual parameters.

如果你不知道,使用这两种方法只是写一点快速测试code,实行每几十万次,时间,看看哪一个是最好的。

If you're not sure, just write a bit of quick test code using both approaches, execute each a few hundred thousand times, and time them to see which is best.

这篇关于从C函数返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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