扩展在C结构

编程入门 行业动态 更新时间:2024-10-27 10:23:33
本文介绍了扩展在C结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近碰到一个同事的code,它看起来像这样来了:

I recently came across a colleague's code that looked like this:

typedef struct A { int x; }A; typedef struct B { A a; int d; }B; void fn(){ B *b; ((A*)b)->x = 10; }

他的解释是,由于结构A 是结构乙的第一个成员,所以 B-> X 将是一样的 B-> AX ,并提供更好的可读性结果。这是有道理的,但是这被认为是好的做法呢?并且将跨平台这项工作?目前,这个运行在GCC的罚款。

His explanation was that since struct A was the first member of struct B, so b->x would be the same as b->a.x and provides better readability. This makes sense, but is this considered good practice? And will this work across platforms? Currently this runs fine on GCC.

推荐答案

是的,这将跨平台工作,但没有的不一定的使它成为一个很好的主意。

Yes, it will work cross-platform, but that doesn't necessarily make it a good idea.

根据ISO C标准, 6.7.2.1结构和联合说明符/ 15 ,也不允许被填充的前的结构的第一个元素

As per the ISO C standard (all citations below are from C11), 6.7.2.1 Structure and union specifiers /15, there is not allowed to be padding before the first element of a structure

此外, 6.2.7兼容型和复合型指出:

两种类型具有兼容的类型,如果其类型是相同的

Two types have compatible type if their types are the same

和它无可争议的是, A 和 A-内-B 类型是完全相同的。

and it is undisputed that the A and A-within-B types are identical.

这意味着内存访问的 A 字段将在两个 A 和 B 类型,会更为明智 B-> AX 这可能是你的应的是使用如果您有关于未来的可维护性任何顾虑。

This means that the memory accesses to the A fields will be the same in both A and B types, as would the more sensible b->a.x which is probably what you should be using if you have any concerns about maintainability in future.

和,虽然你通常会担心严格的类型走样,我不相信在这里适用。它的是的非法别名指针,但标准有具体的例外。

And, though you would normally have to worry about strict type aliasing, I don't believe that applies here. It is illegal to alias pointers but the standard has specific exceptions.

6.5防爆pressions / 7 规定了一些例外者,用脚注:

6.5 Expressions /7 states some of those exceptions, with the footnote:

此列表的目的是,以指定在其中一个对象可能会或可能不会被混叠的那些情况。

The intent of this list is to specify those circumstances in which an object may or may not be aliased.

中列出的例外是:

  • A型的有效对象的类型兼容;
  • 这就需要在这里不关心我们的一些其他异常;和
  • ,其中包括其成员之间的上述类型之一(包括递归,一个子聚集的成员或包含的联合)聚合或联合类型。
  • a type compatible with the effective type of the object;
  • some other exceptions which need not concern us here; and
  • an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union).

这,结合上述结构的填充规则,包括语中的:

That, combined with the struct padding rules mentioned above, including the phrase:

一个指针结构对象,适当的转换,指向其初始成员

A pointer to a structure object, suitably converted, points to its initial member

似乎表明这个例子是专门允许。最核心的一点,我们必须要记住的是,前pression类型((A *)B)是 A * ,而不是 B * 。这使得无限制走样的宗旨的变量。

seems to indicate this example is specifically allowed for. The core point we have to remember here is that the type of the expression ((A*)b) is A*, not B*. That makes the variables compatible for the purposes of unrestricted aliasing.

这是我的标准的相关部分的阅读,我以前错了(一),但我怀疑它在这种情况下。

That's my reading of the relevant portions of the standard, I've been wrong before (a), but I doubt it in this case.

所以,如果你有一个的真正的需要这一点,它的工作不错,但我会在code被记录的任何约束的非常的接近结构以免咬伤的未来。

So, if you have a genuine need for this, it will work okay but I'd be documenting any constraints in the code very close to the structures so as to not get bitten in future.

(一)我的妻子会告诉你,经常和没有太多的提示: - )

(a) As my wife will tell you, frequently and without much prompting :-)

更多推荐

扩展在C结构

本文发布于:2023-11-05 18:53:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!