如何GMP参数C约定转换成更多的东西自然吗? [英] How to convert GMP C parameter convention into something more natural?

查看:141
本文介绍了如何GMP参数C约定转换成更多的东西自然吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我愿做这样的事情:

For example, I would like to do something like this:

#include <gmp.h>
typedef mpz_t Integer;

// 
Integer F(Integer a,Integer b,Integer c,Integer d) {
    Integer ret = times(plus(a,b),plus(c,d));
}

不过,GMP不让我这样做,显然是mpz_t是一个数组,所以我得到的错误:

But, GMP doesn't let me do this, apparently mpz_t is an array, so I get the error:

error: ‘F’ declared as function returning an array

所以不是我会做这样的事情:

So instead I would have to do something like this:

void F(Integer ret,Integer a,Integer b,Integer c,Integer d) {
    Integer tmp1,tmp2;

    plus(tmp1,a,b);     
    plus(tmp2,c,d);     
    times(ret,tmp1,tmp2); 
}

这是不自然的,而不是下面的C(或一般的数学)前pressions可组成合乎逻辑的方式。事实上,你不能在数学般的方式构成任何东西,因为显然你不能返回GMP数字!如果我想写 - 例如 - 一个简单的yacc /野牛风格解析器使用转换简单的语法+, - ,/,*等进入C code。使用GMP执行给定的前pressions现在看来,这会更加困难,因为我将不得不把所有的中间值的轨道。

This is unnatural, and not following the logical way that C (or in general mathematical) expressions can be composed. In fact, you can't compose anything in a math-like way because apparently you can't return GMP numbers! If I wanted to write - for example - a simple yacc/bison style parser that converted a simple syntax using +, -, /, * etc. into C code implementing the given expressions using GMP it seems it would be much more difficult as I would have to keep track of all the intermediate values.

所以,我怎么可以强制GMP在这里服从我的意志,接受一个更合理的语法?我可以放心地欺骗,并投mpz_t为void *,然后在另一端重新构建它放回mpz_t?我从阅读,它不是的真正的周围传递一个数组的文档假设,而仅仅是一个参考,所以为什么不能返回引用呢?有没有做这种方式,我应该在写我自己的计划考虑到一些好声音节目的基础?

So, how can I force GMP to bend to my will here and accept a more reasonable syntax? Can I safely "cheat" and cast mpz_t to a void * and then reconstitute it at the other end back into mpz_t? I'm assuming from reading the documentation that it is not really passing around an array, but merely a reference, so why can't it return a reference as well? Is there some good sound programming basis for doing it this way that I should consider in writing my own program?

推荐答案

从的 gmp.h 的:

typedef __mpz_struct mpz_t[1];

这使得有很大的意义,而是pretty自然的。想想看:一个有
大小为1的阵列允许你处理一个模糊的指针(被称为不透明
参考)及其所有优点:

This makes a lot of sense, and is pretty natural. Think about it: having an array of size 1 allows you to deal with an obscured pointer (known as opaque reference) and all its advantages:

mpz_t number;
DoubleIt(number); /* DoubleIt() operates on `number' (modifies it) as
                     it will be passed as a pointer to the real data */

如果不是一个数组,你必须做一些事情,如:

Were it not an array, you'd have to do something like:

mpz_t number;
DoubleIt(&number);

接着谈到所有的混乱。背后的不透明类型的意图是
隐藏这些,所以你不必担心。而主要原因之一
关注应该清楚:尺寸(这会导致性能)。当然你
不能返回这样结构保存限定于可用的存储器中的数据。什么
这一个(考虑 mpz_t 这里作为一个一流型):

And then it comes all the confusion. The intention behind the opaque type is to hide these, so you don't have to worry about it. And one of the main concerns should be clear: size (which leads to performance). Of course you can't return such struct that holds data limited to the available memory. What about this one (consider mpz_t here as a "first-class" type):

mpz_t number = ...;
number = DoubleIt(number);

您(程序)必须全部数据复制数量,推动它作为一个
参数给你的函数。然后,它需要留有适当的空间,
返回另一个数字更大。

You (the program) would have to copy all the data in number and push it as a parameter to your function. Then it needs to leave appropriate space for returning another number even bigger.

结论:当你要处理的数据间接(用指针)是
最好使用不透明的类型。你只传递一个引用您的
功能,但你可以对他们仿佛整个概念是操作
传递通过引用的(C默认的传递通过引用的)。

Conclusion: as you have to deal with data indirectly (with pointers) it's better to use an opaque type. You'll be passing a reference only to your functions, but you can operate on them as if the whole concept was pass-by-reference (C defaults to pass-by-reference).

这篇关于如何GMP参数C约定转换成更多的东西自然吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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