什么平台有 8 位字符以外的东西? [英] What platforms have something other than 8-bit char?

查看:29
本文介绍了什么平台有 8 位字符以外的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时不时,SO 上有人指出 char(又名字节")不一定是 8 位.

似乎 8 位 char 几乎是通用的.我原以为对于主流平台来说,有必要有一个 8 位 char 以确保其在市场上的生存能力.

现在和过去,哪些平台使用的 char 不是 8 位,为什么它们与正常"的 8 位不同?

在编写代码并考虑跨平台支持(例如对于通用库)时,对于具有非 8 位 char 的平台,值得给予什么样的考虑?

过去我遇到过一些 char 为 16 位的 Analog Devices DSP.我认为 DSP 是一种小众架构.(再说一次,当时手工编码的汇编程序很容易击败可用的 C 编译器可以做的事情,所以我在那个平台上并没有真正获得 C 的经验.)

解决方案

char 在德州仪器 C54x DSP 上也是 16 位,例如在 OMAP2 中出现.还有其他带有 16 位和 32 位 char 的 DSP.我想我什至听说过 24 位 DSP,但我不记得是什么,所以也许是我想象的.

另一个考虑因素是 POSIX 要求 CHAR_BIT == 8.因此,如果您使用的是 POSIX,则可以假设它.如果有人稍后需要将您的代码移植到接近实现的 POSIX,那么恰好有您使用的函数但大小不同的 char,那是他们的不幸.

不过,总的来说,我认为解决这个问题几乎总是比考虑它更容易.只需输入 CHAR_BIT.如果您想要精确的 8 位类型,请使用 int8_t.您的代码将在不提供的实现上编译失败,而不是默默地使用您没想到的大小.至少,如果我遇到一个我有充分理由假设它的案例,那么我会断言它.

Every now and then, someone on SO points out that char (aka 'byte') isn't necessarily 8 bits.

It seems that 8-bit char is almost universal. I would have thought that for mainstream platforms, it is necessary to have an 8-bit char to ensure its viability in the marketplace.

Both now and historically, what platforms use a char that is not 8 bits, and why would they differ from the "normal" 8 bits?

When writing code, and thinking about cross-platform support (e.g. for general-use libraries), what sort of consideration is it worth giving to platforms with non-8-bit char?

In the past I've come across some Analog Devices DSPs for which char is 16 bits. DSPs are a bit of a niche architecture I suppose. (Then again, at the time hand-coded assembler easily beat what the available C compilers could do, so I didn't really get much experience with C on that platform.)

解决方案

char is also 16 bit on the Texas Instruments C54x DSPs, which turned up for example in OMAP2. There are other DSPs out there with 16 and 32 bit char. I think I even heard about a 24-bit DSP, but I can't remember what, so maybe I imagined it.

Another consideration is that POSIX mandates CHAR_BIT == 8. So if you're using POSIX you can assume it. If someone later needs to port your code to a near-implementation of POSIX, that just so happens to have the functions you use but a different size char, that's their bad luck.

In general, though, I think it's almost always easier to work around the issue than to think about it. Just type CHAR_BIT. If you want an exact 8 bit type, use int8_t. Your code will noisily fail to compile on implementations which don't provide one, instead of silently using a size you didn't expect. At the very least, if I hit a case where I had a good reason to assume it, then I'd assert it.

这篇关于什么平台有 8 位字符以外的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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