如果架构不解决字节,C如何应对? [英] How does C cope if architecture doesn't address bytes?

查看:68
本文介绍了如果架构不解决字节,C如何应对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



My K& R 2nd ed在参考手册附录中,A7.4.8 sizeof yield

存储类型对象所需的BYTES数量它的操作数。

如果C只在一台只能处理较大字的机器上运行会怎样?

不应该定义sizeof来返回最小数量的''存储空间

单位''需要存储其操作数类型的对象?


作为一般观点,是否有指导C的哪些方面如果在没有8位字节寻址的机器上运行

会失败吗? IIRC的一些Crays

定义一个char,一个int和一个long int,全部为64位,一些PIC使用12位
字。


我怀疑C是如此明确(或成熟)它如果使用得当它会不会在这些机器上太麻烦。

想知道哪里会出错,如果有的话?


-

TIA,

James


My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields
the number of BYTES required to store an object of the type of its operand.
What happens if C is running on a machine that addresses larger words only?
Shouldn''t sizeof be defined to return the smallest number of ''storage
units'' required to store an object of the type of its operand?

As a general point, is there a guide to what aspects of C would fail if run
on a machine which is not 8-bit byte addressed? IIRC the some of the Crays
define a char, an int and a long int all as 64-bit and some PICs use 12-bit
words.

I have a sneaking suspicion that C is so well defined (or, mature) that it
if used properly it would not run in to too much bother on these machines.
Any idea of where it would go wrong, if anywhere?

--
TIA,
James

推荐答案

Hello James,

James Harris写道:
Hello James,

James Harris wrote:
我的K& R 2nd ed在参考手册附录中,A7.4.8 sizeof产生
存储其操作数类型对象所需的BYTES数。
如果C正在运行,会发生什么一台只能处理较大单词的机器?
不应该定义大小以返回存储其操作数类型对象所需的最小数量的存储单元吗? >
总的来说,我如果在没有8位字节寻址的机器上运行,那么有哪些方面的C会失败? IIRC的一些Crays
定义了一个char,一个int和一个long int,全部为64位,有些PIC使用12位

我有偷偷摸摸的怀疑C是如此明确(或成熟),如果使用得当它不会在这些机器上太麻烦。
想知道它哪里会出错,如果在哪里?
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields
the number of BYTES required to store an object of the type of its operand.
What happens if C is running on a machine that addresses larger words only?
Shouldn''t sizeof be defined to return the smallest number of ''storage
units'' required to store an object of the type of its operand?

As a general point, is there a guide to what aspects of C would fail if run
on a machine which is not 8-bit byte addressed? IIRC the some of the Crays
define a char, an int and a long int all as 64-bit and some PICs use 12-bit
words.

I have a sneaking suspicion that C is so well defined (or, mature) that it
if used properly it would not run in to too much bother on these machines.
Any idea of where it would go wrong, if anywhere?




呃,请注意一件事:字节不一定由8位组成。

在< limits.h>中,CHAR_BIT被定义它告诉你每字节

位的数量。 CHAR_BIT至少为8.


字节是_implementation_

提供的最小可配置内存单位。很可能硬件架构只允许
地址处理器字,比如X位,但编译器为你提供了由b位组成的
a数据类型,X = m * Y,m> 1,并且允许您使用

指针来解决Y位边界处该数据类型的变量。


如果你想要要确定您使用了多少位,请使用

< limits.h>或者,在C99中,(u)intN_t,N = 8,16,32,64,整数数据类型

由< stdint.h>提供。

干杯

Michael

-

电子邮箱:我的是/ at / gmx / dot / de地址。



Er, note one thing: Bytes do not necessarily consist of 8 Bits.
In <limits.h>, CHAR_BIT is defined which tells you the number of
bits per byte. CHAR_BIT is at least 8.

Bytes are the smallest adressable memory units your _implementation_
offers. It may well be that the hardware architecture allows only to
address processor words of, say, X bits but the compiler offers you
a data type consisting of Y bits, X=m*Y, m>1, and enables you to use
pointers to address variables of that data type at Y-bit boundaries.

If you want to be sure how many bits you are working with, use
<limits.h> or, in C99, the (u)intN_t, N=8,16,32,64, integral data types
offered by <stdint.h>.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


在文章< 41 *********************** @ news.zen.co.uk> ;,

James Harris< no.email.please>写道:
In article <41***********************@news.zen.co.uk>,
James Harris <no.email.please> wrote:
我的K& R 2nd ed在参考手册附录中,A7.4.8 sizeof yield
存储其类型对象所需的BYTES数量操作数。


True。

如果C只在一台只能处理大字的机器上运行,会发生什么?


这没问题。

不应该定义大小来返回所需的最小数量的''存储单元''存储其操作数类型的对象?


它已经这样做了。 C中的存储单元是字节。

一般来说,如果在不是8的机器上运行,那么C的哪些方面会失败?位字节寻址?


没有任何失败。并非所有字节都是8位,并且C

标准中使用的字节当然不限于8位。


| 3.6

| 1字节

|可寻址的数据存储单元,足以容纳任何成员

|执行环境的基本字符集

|

| 2注1可以表达每个人的地址

|唯一的对象字节。

|

| 3注2:一个字节由一个连续的位序列组成,

|其数量是实现定义的。最不重要的是

| bit称为低位;最重要的是

|称为高阶位。

IIRC一些Crays
定义一个char,一个int和一个long int全部为64位,一些PIC使用12位
单词。


然后C实现将使用64位或12位作为其字节。

Cray实现可以通过使用非原生地址伪造8位
$ b中添加一个三位字段),性能损失$ b地址)并使用打包/解包代码进行所有字节访问。

我怀疑C是如此定义(或成熟),如果使用得当它会不要在这些机器上烦恼太多。
想知道哪里会出错,如果有的话?
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields
the number of BYTES required to store an object of the type of its operand.
True.
What happens if C is running on a machine that addresses larger words only?
That is no problem.
Shouldn''t sizeof be defined to return the smallest number of ''storage
units'' required to store an object of the type of its operand?
It already does this. The ''storage units'' in C are bytes.
As a general point, is there a guide to what aspects of C would fail if run
on a machine which is not 8-bit byte addressed?
Nothing fails. Not all bytes are eight bits and the byte used in the C
standard is certainly not restricted to be eight bits.

| 3.6
| 1 byte
| addressable unit of data storage large enough to hold any member
| of the basic character set of the execution environment
|
| 2 NOTE 1 It is possible to express the address of each individual
| byte of an object uniquely.
|
| 3 NOTE 2 A byte is composed of a contiguous sequence of bits, the
| number of which is implementation-defined. The least significant
| bit is called the low-order bit; the most significant bit is
| called the high-order bit.
IIRC the some of the Crays
define a char, an int and a long int all as 64-bit and some PICs use 12-bit
words.
Then the C implementation would use 64 bits or 12 bits for its byte. The
Cray implementation could, with a performance penalty, fake eight bit
bytes by using non-native addresses (add a three bit field to the
address) and use pack/unpack code for all byte accesses.
I have a sneaking suspicion that C is so well defined (or, mature) that it
if used properly it would not run in to too much bother on these machines.
Any idea of where it would go wrong, if anywhere?




问题更可能是出现在知道的程序员写的应用程序中。一个字节是8位。


-

G?ran Larsson http://www.mitt-eget.com/


Quoth Goran Larsson 2004年左右-11-13:
Quoth Goran Larsson on or about 2004-11-13:
问题更可能出现在由知道的程序员编写的应用程序中。一个字节是8位。
The problems are much more likely to appear in applications written
by programmers that "knows" that a byte is eight bits.




''char''类型总是只有一个字节宽吗?


- 特伦特



Is the ''char'' type always exactly one byte wide?

-trent


这篇关于如果架构不解决字节,C如何应对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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