c - C 中的接口(interface)

标签 c struct language-lawyer undefined-behavior

我在设计应用程序时遇到了实现问题。我有以下结构定义:

app.h:

struct application_t{
    void (*run_application)(struct application_t*);
    void (*stop_application)(struct application_t*);
}

struct application_t* create();

当我试图“实现”这个application_t 时,问题就来了。我倾向于定义另一个结构:

app.c:

struct tcp_application_impl_t{
    void (*run_application)(struct application_t*);
    void (*stop_application)(struct application_t*);
    int client_fd;
    int socket_fd;
}

struct application_t* create(){
     struct tcp_application_impl_t * app_ptr = malloc(sizeof(struct tcp_application_impl_t));
     //do init
     return (struct application_t*) app_ptr;
}

所以如果我按如下方式使用它:

#include "app.h"

int main(){
    struct application_t *app_ptr = create();
    (app_ptr -> run_application)(app_ptr);    //Is this behavior well-defined?
    (app_ptr -> stop_application)(app_ptr);   //Is this behavior well-defined?
}

让我困惑的问题是,如果我调用 (app_ptr -> run_application)(app_ptr); 会产生 UB。

app_ptr 的“静态类型”是struct application_t*,但“动态类型”是struct tcp_application_impl_t*struct application_tstruct tcp_application_t 与 N1570 6.2.7(p1) 不兼容:

there shall be a one-to-one correspondence between their members such that each pair of corresponding members are declared with compatible types

在这种情况下显然不是这样。

能否请您提供对解释该行为的标准的引用?

最佳答案

您的两个结构不兼容,因为它们是不同的类型。您已经找到“兼容类型”一章,它定义了使两个结构兼容的原因。当您使用指向错误类型的指针访问这些结构时,UB 稍后出现,根据 6.5/7 严格违反别名。

解决这个问题的明显方法是这样的:

struct tcp_application_impl_t{
    struct application_t app;
    int client_fd;
    int socket_fd;
}

现在类型可能会出现别名,因为 tcp_application_impl_t 是一个在其成员中包含一个 application_t 的集合。

另一种明确定义的方法是使用隐藏在 C17 6.5.2.3/6 中的偷偷摸摸的特殊规则“union 公共(public)初始序列”:

One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

这将允许您使用您声明的原始类型。但是在同一个翻译单元的某个地方,你必须添加一个虚拟 union 类型定义来利用上面的规则:

typedef union
{
  struct application_t app;
  struct tcp_application_impl_t impl;
} initial_sequence_t;

你不需要实际使用这个 union 的任何实例,它只需要坐在那里可见。这告诉编译器这两种类型允许别名,只要它们的公共(public)初始序列就可以。在你的例子中,它意味着函数指针而不是 tcp_application_impl_t 中的尾随变量。

编辑:

免责声明。常见的初始序列技巧显然有点争议,编译器用它做了委员会预期之外的其他事情。并且在 C 和 C++ 中的工作方式可能不同。参见 union 'punning' structs w/ "common initial sequence": Why does C (99+), but not C++, stipulate a 'visible declaration of the union type'?

关于c - C 中的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54570204/

相关文章:

将 IP 从 C 字符串转换为无符号整数?

go - 如何在Golang中的struct中访问嵌套字段

c - Qsort 对指向另一个结构的结构数组进行排序

c++ - 类模板的成员模板的成员模板的显式模板函数特化是否有效?

c++ - 正在写入 &str[0] 缓冲区(std :string) well-defined behaviour in C++11?

c++ - 试图理解 C++ 标准中的 [class.qual]/2

C: char* 不可修改

c - 尝试将文本文件读入数组而不在 C 中重复

复制具有不同大小的缓冲区的文件以进行读取和写入

arrays - 如何过滤结构数组?