C 编译器标志忽略符号

发布于 2024-08-03 07:50:05 字数 265 浏览 6 评论 0原文

我目前正在处理从第三方承包商购买的代码。一个结构体有一个 unsigned char 字段,而它们将该字段传递给的函数需要一个signed char。编译器不喜欢这样,因为它认为它们是不匹配的类型。然而,它显然是为该承包商编译的。一些谷歌搜索告诉我“char 对象是否可以保存负值是由实现定义的”。承包商的编译器是否可以基本上忽略有符号/无符号类型并将它们视为相同的?或者是否有一个编译器标志可以将它们视为相同的?

C 不是我最擅长的语言——只需看看我的用户页面上的标签——所以任何帮助将不胜感激。

I am currently dealing with code purchased from a third party contractor. One struct has an unsigned char field while the function that they are passing that field to requires a signed char. The compiler does not like this, as it considers them to be mismatched types. However, it apparently compiles for that contractor. Some Googling has told me that "[i]t is implementation-defined whether a char object can hold negative values". Could the contractor's compiler basically ignore the signed/unsigned type and treat them the same? Or is there a compiler flag that will treat them the same?

C is not my strongest language--just look at my tags on my user page--so any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘紸啶 2024-08-10 07:50:05

实际上,charsigned charunsigned char是三种不同的类型。根据标准 (ISO/IEC 9899:1990):

6.1.2.5 类型

...

三种类型charsigned char
unsigned char统称为
字符类型

(例如,在 C++ 中,如果您有 char 参数,您必须(或至少应该)编写具有 char 参数的三个变体的重写函数)

普通 char 可能会被编译器视为有符号或无符号,但标准说(也在 6.1 中) .2.5):

声明为 char 类型的对象是
足够大以存储任何成员
基本执行字符集。如果
所需来源的成员
5.2.1中的字符集存储在
char 对象,其值有保证
保持积极的态度。如果有其他数量
存储在 char 对象中,
行为是实现定义的:
这些值被视为
有符号或非负整数。

声明为signed char类型的对象与“普通”char对象占用相同的存储空间。

5.2.1 中提到的字符是 AZ、az、0-9、空格、制表符、换行符和以下 29 个图形字符:

! " # % & ' ( ) * + , - . / :
; < = > ? [ \ ] ^ _ { | } ~ 

回答

所有这些我解释为基本上意味着值小于 128 的 ascii 字符保证是积极的。因此,如果存储的值始终小于 128,那么它应该是安全的(从保值的角度来看),尽管不是很好的做法。

Actually char, signed char and unsigned char are three different types. From the standard (ISO/IEC 9899:1990):

6.1.2.5 Types

...

The three types char, signed char and
unsigned char are collectively called
the character types.

(and in C++ for instance you have to (or at least should) write override functions with three variants of them if you have a char argument)

Plain char might be treated signed or unsigned by the compiler, but the standard says (also in 6.1.2.5):

An object declared as type char is
large enough to store any member of
the basic execution character set. If
a member of the required source
character set in 5.2.1 is stored in a
char object, its value is guarantied
to be positive. If other quantities
are stored in a char object, the
behavior is implementation-defined:
the values are treated as either
signed or nonnegative integers.

and

An object declared as type signed char occupies the same amount of storage as a ''plain'' char object.

The characters referred to in 5.2.1 are A-Z, a-z, 0-9, space, tab, newline and the following 29 graphic characters:

! " # % & ' ( ) * + , - . / :
; < = > ? [ \ ] ^ _ { | } ~ 

Answer

All of that I interpret to basically mean that ascii characters with value less than 128 are guarantied to be positive. So if the values stored always are less than 128 it should be safe (from a value preserving perspective), although not so good practice.

木緿 2024-08-10 07:50:05

这是依赖于编译器的。例如,在 VC++ 中,如果该选项指示默认使用 unsigned char,则定义一个编译器选项和一个相应的 _CHAR_UNSIGNED 宏。

This is compiler-dependent. For example, in VC++ there's a compiler option and a corresponding _CHAR_UNSIGNED macro defined if that option instructs to use unsigned char by default.

又怨 2024-08-10 07:50:05

我认为您正在谈论 signed charunsigned char 类型的字段,因此它们显然是错误的。如果其中之一只是 char,它可能会在承包商使用的任何编译器中匹配(IIRC,它是实现定义的 char 是否是 signedunsigned),但不是你的。在这种情况下,您也许可以使用命令行选项或其他方式来更改您的选项。

或者,承包商可能正在使用编译器或编译器选项,这允许他在忽略错误或警告的同时进行编译。你知道他有什么样的编译环境吗?

无论如何,这都不是好的 C。如果其中一种类型只是 char,那么它依赖于实现定义的行为,因此不可移植。如果不是,那就大错特错了。我会和承包商一起解决这个问题。

I take it that you're talking about fields of type signed char and unsigned char, so they're explicitly wrong. If one of them was simply char, it might match in whatever compiler the contractor is using (IIRC, it's implementation-defined whether char is signed or unsigned), but not in yours. In that case, you might be able to get by with a command-line option or something to change yours.

Alternatively, the contractor might be using a compiler, or compiler options, that allow him to compile while ignoring errors or warnings. Do you know what sort of compilation environment he has?

In any case, this is not good C. If one of the types is just char, it relies on implementation-defined behavior, and therefore isn't portable. If not, it's flat wrong. I'd take this up with the contractor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文