简体   繁体   English

大小为 1 的 Typedef(匿名)结构数组

[英]Typedef (anonymous) struct array of size 1

So recently I've been poking around in the internals of GMP for a project I'm working on, and I've encountered a construct that makes very little sense to me: (taken from the header for version 6.2.1)所以最近我一直在为我正在从事的项目研究 GMP 的内部结构,我遇到了一个对我来说意义不大的构造:(取自 6.2.1 版的标题)

/* For reference, note that the name __mpz_struct gets into C++ mangled
   function names, which means although the "__" suggests an internal, we
   must leave this name for binary compatibility.  */
typedef struct
{
  int _mp_alloc;        /* Number of *limbs* allocated and pointed
                   to by the _mp_d field.  */
  int _mp_size;         /* abs(_mp_size) is the number of limbs the
                   last field points to.  If _mp_size is
                   negative this is a negative number.  */
  mp_limb_t *_mp_d;     /* Pointer to the limbs.  */
} __mpz_struct;

#endif /* __GNU_MP__ */


typedef __mpz_struct MP_INT;    /* gmp 1 source compatibility */
typedef __mpz_struct mpz_t[1];

On the final line here, we typedef mpz_t to be an array of size 1 containing __mpz_struct s.在这里的最后一行,我们将mpz_t为一个大小为 1 的数组,其中包含__mpz_struct s。 This is very strange to me, and I've never seen a construction like this before.这对我来说很奇怪,我以前从未见过这样的建筑。 I understand that this results in mpz_t effectively being a pointer to an __mpz_struct , but is this equivalent or not to我明白,这导致mpz_t有效是一个指向__mpz_struct ,但是这是相当于或不

typedef __mpz_struct* mpz_t;

Could somebody explain the logic behind this?有人可以解释这背后的逻辑吗?

With that typedef , a variable declaration like使用那个typedef ,一个变量声明像

mpz_t foo;

is equivalent to相当于

__mpz_struct foo[1];

This will only be converted to这只会转换为

__mpz_struct *foo;

if the declaration is in a function parameter list.如果声明在函数参数列表中。 Otherwise, it's a ordinary array declaration.否则,它是一个普通的数组声明。

Given typedef __mpz_struct mpz_t[1];给定typedef __mpz_struct mpz_t[1]; , you can declare an actual mpz_t object with mpz_t foo; , 你可以用mpz_t foo;声明一个实际的mpz_t对象mpz_t foo; . . This will reserve memory for an mpz_t .这将为mpz_t保留内存。

In contrast, if the type were typedef __mpz_struct *mpz_t;相反,如果类型是typedef __mpz_struct *mpz_t; , then mpz_t foo; , 然后mpz_t foo; would only give you a pointer.只会给你一个指针。 There would be no space for an mpz_t . mpz_t将没有空间。 You would have to allocate it, and you would have to free it later.您必须分配它,然后必须释放它。 So more code is required, and it would be a nuisance.所以需要更多的代码,这会很麻烦。

At the same time, when an mpz_t is passed to a function, only its address is passed, due to the automatic conversion of arrays to pointers.同时,当将mpz_t传递给函数时,由于数组自动转换为指针,因此仅传递其地址。 This allows writing mpz_add(z, x, y);这允许编写mpz_add(z, x, y); , which is less cluttered than mpz_add(&z, &x, &y); ,它比mpz_add(&z, &x, &y);mpz_add(&z, &x, &y); . . There may be arguments among programmers about whether it is bug-prone to use a type definition that effectively results in a reference to an object being passed to a function when the code nominally looks like just a value is passed, but this style may be more suitable for the type of programming done with GMP.程序员之间可能会争论,当代码名义上看起来只是传递一个值时,使用类型定义有效地导致对对象的引用传递给函数是否容易出错,但这种风格可能更多适用于使用 GMP 完成的编程类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM