char的签名是否是接口问题? [英] Is the signedness of char an interface issue?

查看:197
本文介绍了char的签名是否是接口问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个功能

void foo(char *)

在内部,需要将其输入视为NUL终止字节块(例如,它是字符串上的散列函数)。我可以在函数中将参数转换为 unsigned char * 。我也可以将声明更改为

which, internally, needs to treat its input as a block of NUL-terminated bytes (say, it's a hash function on strings). I could cast the argument to unsigned char* in the function. I could also change the declaration to

void foo(unsigned char *)

现在,鉴于 char signed char unsigned char 三种不同类型,这是否构成界面变更,根据C中术语界面的任何合理定义?

Now, given that char, signed char and unsigned char are three different types, would this constitute an interface change, under any reasonable definition of the term "interface" in C?

(此问题旨在解决<另一个问题引发了一个href =https://stackoverflow.com/questions/4003347/problem-with-hashing-function-c/4003353#4003353>讨论。我有我的意见,但不接受一个答案,直到一个人通过其他人的投票成为赢家。)

(This question is intended to settle a discussion raised by another question. I have my opinions, but will not accept an answer until one comes up as a "winner" by the votes of others.)

推荐答案

根据ISO / IEC 9899: TC3,

According to ISO/IEC 9899:TC3,


  • 通过不兼容类型的表达式调用函数是未定义的行为(6.5.2.2§9)

  • 兼容函数类型必须具有兼容的参数类型(6.7.5.3§15)

  • 兼容的指针类型必须指向兼容的类型(6.7.5.1§2)

  • char signed char unsigned char 是不同的基本类型(6.2.5§14)因此不兼容(6.2.7§1),这也在第35页的脚注35中明确提到

  • calling a function through an expression of incompatible type is undefined behaviour (6.5.2.2 §9)
  • compatible function types must have compatible parameter types (6.7.5.3 §15)
  • compatible pointer types must point to compatible types (6.7.5.1 §2)
  • char, signed char and unsigned char are different basic types (6.2.5 §14) and thus incompatible (6.2.7 §1), which is also explicitly mentioned in footnote 35 on page 35

所以是的,这显然是对编程界面的改变。

So yes, this is clearly a change to the programming interface.

然而,作为 char * signed char * unsigned char * 在任何理智的C语言实现中具有相同的表示和对齐要求,二进制接口将保持不变。

However, as char *, signed char * and unsigned char * will have identical representations and alignment requirements in any sane implementation of the C language, the binary interface will remain unchanged.

这篇关于char的签名是否是接口问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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