- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 C89 中实现了一个二叉树,我试图通过组合在所有节点结构之间共享公共(public)属性。因此我有以下代码:
enum foo_type
{
FOO_TYPE_A,
FOO_TYPE_B
};
struct foo {
enum foo_type type;
};
struct foo_type_a {
struct foo base;
struct foo * ptr;
};
struct foo_type_b {
struct foo base;
char * text;
};
我在所有结构定义中包含一个类型为 struct foo
的成员作为它们的初始成员,以便提供对 enum foo_type
持有的值的访问,而不管结构如何类型。为实现这一点,我希望指向结构对象的指针指向其初始成员,但我不确定这种假设在这种情况下是否成立。对于 C99,标准规定如下(参见 ISO/IEC 9899:1999 6.7.2.1 §13)
A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.
尽管所有结构共享一个共同的 struct foo
对象作为它们的初始成员,填充开始发挥作用。 struct foo
只有一个int
大小的成员,struct foo_type_a
和struct foo_type_b
都包含指针成员,在某些情况下会增加对齐,从而增加填充。
因此,考虑到这种情况,C 编程语言(C89 或任何后续版本)是否确保通过指向对象的指针访问 struct foo::type
的值是安全的,无论该对象的类型为 struct foo
或包含类型为 struct foo
的对象作为其第一个成员,例如 struct foo_type_a
或 struct foo_type_b
?
最佳答案
正如您自己引用的 C 标准,您所描述的内容受 C99 及更高版本的支持。
C89 似乎也支持它,因为您引用的语言自 1988 年以来就已经存在于 ANSI-C 文档中:
3.5.2.1 Structure and union specifiers
...
Within a structure object, the non-bit-field members and the unitsin which bit-fields reside have addresses that increase in the orderin which they are declared. A pointer to a structure object, suitablycast, points to its initial member (or if that member is a bit-field,then to the unit in which it resides), and vice versa. There maytherefore be unnamed holes within a structure object, but not at itsbeginning, as necessary to achieve the appropriate alignment.
关于c - 结构包装 : how to add struct members at the beginning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55560382/
我有一个数组 items[] items[] 中的每一项都是一个结构体。 item 有键 id、date、value(即 item.id、item.date、item.value) 我想使用 Stru
我想存储 100 名员工。 RollNo,姓名,工资,时间(各种数据,我无法在这里解释,但你可以看下面的代码片段来理解 main() { struct day { int hour
这个问题在这里已经有了答案: storage size of ‘names’ isn’t known (3 个答案) 关闭 5 年前。 我正在尝试蓝牙编程,遇到了这个我不明白的问题。基本上,当我使用
这是一个奇怪的事情: 我有一个结构,它包含指向相同类型结构的指针和指向其他类型结构的指针,以及一些其他值。 struct animal { struct animal * father;
我有一个结构定义如下(名称不同) struct str1 { int field1; struct str2; } 我在一个函数中有一个*str1。我想要一个指向 str2 的指针。 所以
DISK_DETECTION_INFO is defined as有什么原因吗? typedef struct _DISK_DETECTION_INFO { DWORD Size
我正在尝试打包一个字符串和一个字符串的长度。 fmt = '
我在创建结构时遇到问题。 我的结构: public struct Device: Codable { let data: DeviceData let meta: Meta? } pu
struct Item { var name:String? var type:String? var value:Int? var tag:Int? } ... ..
// NewReaderSize returns a new Reader whose buffer has at least the specified 43 // size. If the ar
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 3 年前。 在下面的 C++ 片段中, 如何基于 TwoInts 结构中的
#include struct Header { unsigned long long int alignment; }; int main(void) { struct Heade
我有一个目前看起来像这样的结构(缩写为仅显示基本部分): typedef struct { uint32_t baudrate; ... some other internally u
对此没有太多解释,这就是我所拥有的: public struct PACKET_HEADER { public string computerIp; publi
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
struct S1 { char c; int i; }; struct S3 { char c1; struct S1 s; double c2; }; 我正
我有一个名为 Parameter 的协议(protocol): protocol Parameter { var name: String { get } var unit: Unit
有 2 个 struct 定义 A 和 A。我知道 struct A 可以包含指向 struct A 的 POINTER 但我不明白为什么 struct A 不能包含struct A(不是指针) 最佳
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
为了说明这一点,这里有一个小的不可变结构和一个更新它的函数: (struct timeseries (variable observations) #:transparent) (define (ad
我是一名优秀的程序员,十分优秀!