C 中的空结构 [英] Empty structs in C

查看:35
本文介绍了C 中的空结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://c0x.coding-guidelines.com/6.7.2.1.html:

1401 如果结构声明列表不包含命名成员,则行为未定义.

1401 If the struct-declaration-list contains no named members, the behavior is undefined.

这是否意味着以下内容是非法的?

Does this mean that the following is illegal?

struct C { };

或者是什么意思?

我使用了一个 Convention=>C Ada 指针指向一个空结构(实际上是空的 Ada 记录)来代替 void* 在我的 C 库绑定中(因为 Ada 中没有 void*).我想知道这是否不正确.

I used a Convention=>C Ada pointer to an empty struct (in fact empty Ada record) to serve instead void* in my bindings of a C library (because there is no void* in Ada). I wonder if this is incorrect.

我有什么误解?

另请参阅不要将没有非零大小字段的结构传递或返回给外部 (C) 函数.根据 C11 6.7.2.1p8,这是未定义的行为."在 https://dlang.org/spec/struct.html

See also "Do not pass or return structs with no fields of non-zero size to extern (C) functions. According to C11 6.7.2.1p8 this is undefined behavior." at https://dlang.org/spec/struct.html

推荐答案

C 中很少有不合法的,这意味着您不能这样做.相反,C 标准并未定义许多事情,这意味着,如果您执行这些操作,C 标准不会说明会发生什么.没有命名成员的结构就是这样.

Very little in C is illegal, meaning you cannot do it. Rather, many things are not defined by the C standard, meaning that, if you do them, the C standard does not say what will happen. Structures with no named members are such a thing.

C 2018 6.7.2.1 8 部分说:

C 2018 6.7.2.1 8 says, in part:

如果 struct-declaration-list 不直接或通过匿名结构或匿名联合包含任何命名成员,则行为未定义.

If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined.

如果你需要一些东西作为未知类型"并且不想使用void,那么你可以声明一个标签已知但内容未知的结构,如下所示:

If you need something to serve as an "unknown type" and do not want to use void, then you can declare a structure whose tag is known but whose contents are not, as with:

struct foo;

然后你可以使用指向这种结构的指针(例如,你可以定义一个指针struct foo *p;),但是编译器不会知道这种结构的大小或内容,所以它除了传递指针和询问外部例程(它们知道内容)之外,无法帮助您分配它们、管理它们的数组或以其他方式使用它们.

Then you can use pointers to such structures (for example, you can define a pointer struct foo *p;), but the compiler will not know the size or contents of such structures, so it cannot help you allocate them, manage arrays of them, or otherwise use them other than by passing pointers around and asking external routines (which do know about the contents).

通常,在一个模块中的 C 例程和另一模块中的 C 例程之间的操作中:

Normally, in operations between C routines in one module and C routines in another module:

  • 在一个不知道结构内容的源模块中,您将使用指针 (struct foo *).
  • 另一个源模块(通常是某种软件库)会知道结构的内容.它会在它自己的源代码中用 struct foo {/* 各种事物 */}; 定义完整的结构,并且它会在这个结构上为第一个模块执行服务.
  • 在两个这样的 C 模块之间,C 标准的规则将定义行为.
  • In one source module that did not know about the contents of the structure, you would use a pointer (struct foo *).
  • Another source module (often a software library of some sort) would know about the contents of the structure. It would, inside its own source code, define the full structure with struct foo { /* various things */ };, and it would perform services on this structure for the first module.
  • Between two such C modules, the rules of the C standard would define the behavior.

由于您在 C 和 Ada 之间进行交互,因此这些交互的规则必须由您的 C 和 Ada 实现提供.

Since you are interfacing between C and Ada, the rules for those interactions must be provided by your C and Ada implementations.

这篇关于C 中的空结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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