c - 将 f(mystruct *a) 更改为 f(const mystruct *a) 会破坏 C 中的 API/ABI 吗?

标签 c api constants compatibility abi

1: void f(mystruct *a)
2: void f(const mystruct *a)

从 1->2 更改函数签名会破坏 C 中的 API/ABI 吗?
改变 2->1 怎么样?

最佳答案

来自 C99 标准 6.2.5/26“类型”:

pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements.

因此,从 1 到 2,ABI/API 不应受到影响。(API 不会改变,因为指向非 const 限定类型的指针可能会转换为指向 const 限定版本的指针类型 - 6.3.2.3/2“转换 - 指针”)。

但是,如果从 2 变为 1,则 API 会发生变化,因为指向 const 对象的指针无法隐式转换为指向非 const 对象的指针。以下代码可以在版本 2 下编译,但不能在版本 1 下编译:

static const mystruct foo;

f(&foo);

关于c - 将 f(mystruct *a) 更改为 f(const mystruct *a) 会破坏 C 中的 API/ABI 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5083765/

相关文章:

c - 如何确保枚举和数组在编译时具有相同的条目?

c++ - 在 LuaSQL 代码上断言 C/C++

c# - C# 中的卷影复制 (VSS) 示例?

c++ - 将指向类的指针 append 到 QList QList<Class* >

c - 如何将 PFS 添加到用 c 和 openssl 编写的套接字服务器

c - 使用 ncurses 显示数独网格

javascript - "undefined is not a function"使用回调

java - 请建议java邮件api

c++ - const 在函数中的位置

c++ - C++ 中的常量混淆