关于typedef的单实例数组的几个问题 [英] Some questions about a single-instance array in typedef

查看:144
本文介绍了关于typedef的单实例数组的几个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我细读使用使用GNU多precision(GMP)库code任意长度的整数部分code。该类型的MP整数 mpz_t 在gmp.h头文件中定义。

I was perusing some code using arbitrary-length integers using the GNU Multi-Precision (GMP) library code. The type for a MP integer is mpz_t as defined in gmp.h header file.

不过,我有关于这个库定义的 mpz_t 键入较低级别的定义有些问题。在头code:

But, I've some questions about the lower-level definition of this library-defined mpz_t type. In the header code:

/* THIS IS FROM THE GNU MP LIBRARY gmp.h HEADER FILE */
typedef struct
{
    /* SOME OTHER STUFF HERE */
} __mpz_struct;

typedef __mpz_struct mpz_t[1];

第一个问题:是否 [1] 联想到 __ mpz_struct ?换句话说,就是的typedef 定义 mpz_t 键入作为 __ mpz_struct 阵列与一个发生?

First question: Does the [1] associate with the __mpz_struct? In other words, is the typedef defining a mpz_t type as a __mpz_struct array with one occurrence?

第二个问题:为什么数组? (为什么只出现一次?)这是其中的一个结构黑客的我听说过?

Second question: Why the array? (And why only one occurrence?) Is this one of those struct hacks I've heard about?

第三个问题(也许是间接的涉及到第二个问题):为 mpz_init_set的GMP文件(mpz_t,unsigned long int类型)函数表示,用它作为传递逐只有价值,虽然一会的假设的该函数将被调用的函数内修改其内容(并因此将需要传递通过引用)的语法。请参阅我的code:

Third question (perhaps indirectly related to second question): The GMP documentation for the mpz_init_set(mpz_t, unsigned long int) function says to use it as pass-by-value only, although one would assume that this function would be modifying its contents within the called function (and thus would need pass-by-reference) syntax. Refer to my code:

/* FROM MY CODE */
mpz_t fact_val;                /* declaration */
mpz_init_set_ui(fact_val, 1);  /* Initialize fact_val */

请问单发生阵列启用传递通过引用自动(由于阵列/指针语义在C崩溃)?我坦率地承认我有点过分析,但我确实很想有这方面的讨论。谢谢!

Does the single-occurrence array enable pass-by-reference automatically (due to the breakdown of array/pointer semantics in C)? I freely admit I'm kinda over-analyzing this, but I'd certainly love any discussion on this. Thanks!

推荐答案

* 第一个问题:是否 [1] 联想到__mpz_struct?换句话说,就是定义一个mpz_t类型作为__mpz_struct阵列与一个发生的typedef? *

*First question: Does the [1] associate with the __mpz_struct? In other words, is the typedef defining a mpz_t type as a __mpz_struct array with one occurrence?*

第二个问题:为什么数组? (为什么只出现一次?)是这些结构的黑客这一块我听说过?

我就不清楚了。不知道,但一种可能性是作者想使该被引用自动传递,或者,是,可能是黑客结构的对象。如果你看到一个 mpz_t 对象作为结构的最后一个成员,那么几乎可以肯定它的结构黑客。看起来像一个分配

Beats me. Don't know, but one possibility is that the author wanted to make an object that was passed by reference automatically, or, "yes", possibly the struct hack. If you ever see an mpz_t object as the last member of a struct, then "almost certainly" it's the struct hack. An allocation looking like

malloc(sizeof(struct whatever) + sizeof(mpz_t) * some_number)`

将是一个大破绽。

would be a dead giveaway.

请问单发生阵列启用传递通过引用自动...?

啊哈,你理解了它了。 是,一个可能的原因是在更复杂的引用为代价来简化通通过引用

Aha, you figured it out too. "Yes", one possible reason is to simplify pass-by-reference at the expense of more complex references.

我想另一种可能性是,一些在数据模型或算法,笔者改变要找到每一个参考,并改变它以某种方式。在这样一种类型的变化要离开这个项目具有相同的基本类型,但错误每未转换的参考。

I suppose another possibility is that something changed in the data model or the algorithm, and the author wanted to find every reference and change it in some way. A change in type like this would leave the program with the same base type but error-out every unconverted reference.

这篇关于关于typedef的单实例数组的几个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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