C解释器和sizeof运算符 [英] C Interpreter and sizeof operator

查看:64
本文介绍了C解释器和sizeof运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人正在编写C语言翻译器,那么标准

标准中是否有任何要求sizeof运算符为

产生相同值的两个不同的变量相同的类型?


让我们假设解释器确实符合范围值

,比如说类型为int,但是为变量分配存储空间基于价值的
。所以,对于两个变量foo和bar


int foo = 0; / * interpreter分配两个字节* /

int bar = 200000000; / * interpreter分配四个字节* /


标准是否要求sizeof foo == sizeof bar因此

使这个分配方案被破坏,除非隐藏在某些方式?

或者尺寸操作员完全可以接受不同的结果吗?


问候,Oz

-

答:因为它会破坏人们通常阅读文字的顺序。

问:为什么顶级发布这么糟糕的事情?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

解决方案



" ozbear" <盎司**** @ bigpond.com>在消息中写道

news:438cdcef.9338968@news-server ...

如果有人正在编写C语言翻译,标准中是否有任何标准
标准这需要sizeof运算符为两个相同类型的不同变量产生相同的值?


是的。

我们假设解释器确实符合范围值
,例如,输入int,但是分配存储空间对于基于变量的变量值。那么,对于两个变量foo和bar

int foo = 0; / * interpreter分配两个字节* /
int bar = 200000000; / * interpreter分配四个字节* /


注意类型''int''只需要最大值32767

(但允许更大)。


另请注意,一个字节的大小(类型''char'')必须至少为8

位(但是允许更大)。

标准是否要求sizeof foo == sizeof bar


是的。每种类型必须有一个特定的大小,在你的

例子中,''foo''和'bar''的可能范围必须是

相同。因此,除非以某种方式隐藏,否则这个分配方案会被破坏吗?
或者对于sizeof运算符来说,不同的结果是否完全可以接受?




不,不适用于同类型的物体。你为什么要这样做

呢?如果我后面的代码想写的话怎么办?

foo = 10000;


-Mike


< blockquote> ozbear写道:

如果有人正在编写C解释器,那么标准
标准中是否有任何要求sizeof运算符为两个不同的变量产生相同的值相同类型的?

是的。 (我知道这是因为我最近查了同样的事情。)如果x和
$ b $具有相同的类型,sizeof x = sizeof y必须毫无例外地持有。


6.5.3.4:sizeof运算符产生其操作数的大小(以字节为单位),

可以是表达式或类型的带括号的名称。根据操作数的类型确定大小

。结果是整数。如果操作数的

类型是可变长度数组类型,则操作数是

已计算;否则,不评估操作数,结果是一个

整数常量。


注意操作数的*值*是无关紧要的。只有它的*类型*很重要。

因此,对象的大小可能*不依赖于它的值,而只取决于它的类型

。如果x和y的类型为''int'',那么sizeof x = sizeof(int)和

sizeof y = sizeof(int),因此sizeof x = sizeof y。如果

你违反了这个,就会发生糟糕的事情。

让我们假设解释器确实符合范围值
,例如,输入int,但是根据值的值为变量分配存储空间。那么,对于两个变量foo和bar

int foo = 0; / * interpreter分配两个字节* /
int bar = 200000000; / * interpreter分配四个字节* /

标准是否要求sizeof foo == sizeof bar,从而使这个分配方案被破坏,除非以某种方式隐藏?


是的。


如果应用程序无法支付
旅行,你可以尝试捏造它错误的大小,但在不违反

标准的情况下这样做很棘手,并且在翻译中它不太可能具有任何价值。只需

为整数选择一个常量大小(4个字节恰好是一个非常常见的)。

或者对于sizeof运算符来说它是完全可以接受的
结果?



不,它不是。


S.


在文章< 438cdcef.9338968@news-server>, oz****@bigpond.com (ozbear)

写道:

如果有人正在编写C解释器,那么标准
标准中是否有任何要求sizeof运算符产生相同值的
两个相同类型的不同变量?

让我们假设解释器确实符合范围值
,例如,输入int,但是为它分配存储空间变量基于它们的价值。那么,对于两个变量foo和bar

int foo = 0; / * interpreter分配两个字节* /
int bar = 200000000; / * interpreter分配四个字节* /

标准是否要求sizeof foo == sizeof bar从而使这个分配方案破坏,除非以某种方式隐藏?
或者是sizeof运算符完全可以接受不同的结果吗?




sizeof(foo)必须等于sizeof(bar)。


memcpy(& foo,& bar,sizeof(foo))必须具有与

简单赋值foo = bar完全相同的效果。并且该任务必须有效,因此你的
解释器必须将分配给foo的内存从2字节更改为4

字节。


如果我写一个函数


void f(int * p,int value){* p = value; }


然后调用


f(& foo,2000000000)


必须有效。基本上,一切都必须由C

标准保证。只要翻译确保所有内容都能正常运行,就可以随意做任何事情。


If one were writing a C interpreter, is there anything in the standard
standard that requires the sizeof operator to yield the same value for
two different variables of the same type?

Let''s assume that the interpreter does conform to the range values
for, say, type int, but allocates storage for the variables based
on their value. So, for two variables foo and bar

int foo = 0; /* interpreter allocates two bytes */
int bar = 200000000; /* interpreter allocates four bytes */

Does the standard require that sizeof foo == sizeof bar thereby
making this allocation scheme broken, unless hidden in some way?
Or is it perfectly acceptable for the sizeof operator to different
results?

Regards, Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

解决方案


"ozbear" <oz****@bigpond.com> wrote in message
news:438cdcef.9338968@news-server...

If one were writing a C interpreter, is there anything in the standard
standard that requires the sizeof operator to yield the same value for
two different variables of the same type?
Yes.

Let''s assume that the interpreter does conform to the range values
for, say, type int, but allocates storage for the variables based
on their value. So, for two variables foo and bar

int foo = 0; /* interpreter allocates two bytes */
int bar = 200000000; /* interpreter allocates four bytes */
Note that type ''int'' is only required to have a max value of 32767
(but is allowed to be larger).

Also note that the size of a byte (type ''char'') must be at least 8
bits (but is allowed to be larger).

Does the standard require that sizeof foo == sizeof bar
Yes. Each type must have a specific size, iow in your
example, the possible ranges of ''foo'' and ''bar'' must be
the same.
thereby
making this allocation scheme broken, unless hidden in some way?
Or is it perfectly acceptable for the sizeof operator to different
results?



No, not for objects of the same type. Why would you want to do this
anyway? What if I later in the code wanted to write:
foo = 10000;

-Mike


ozbear wrote:

If one were writing a C interpreter, is there anything in the standard
standard that requires the sizeof operator to yield the same value for
two different variables of the same type?
Yes. (I know this because I looked up the exact same thing lately.) If x and
y have the same type, sizeof x = sizeof y must hold without exception.

6.5.3.4: "The sizeof operator yields the size (in bytes) of its operand,
which may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If the
type of the operand is a variable length array type, the operand is
evaluated; otherwise, the operand is not evaluated and the result is an
integer constant."

Note that the *value* of the operand is irrelevant. Only its *type* matters.
Therefore the size of an object may *not* depend on its value, but only on
its type. If x and y are of type ''int'', then sizeof x = sizeof(int) and
sizeof y = sizeof(int), therefore sizeof x = sizeof y. Bad Things happen if
you violate this.
Let''s assume that the interpreter does conform to the range values
for, say, type int, but allocates storage for the variables based
on their value. So, for two variables foo and bar

int foo = 0; /* interpreter allocates two bytes */
int bar = 200000000; /* interpreter allocates four bytes */

Does the standard require that sizeof foo == sizeof bar thereby
making this allocation scheme broken, unless hidden in some way?
Yes.

You can try and fudge this in cases where the application cannot possibly
trip over the wrong size, but it''s tricky to do this without violating the
standard, and in an interpreter it''s very unlikely to be of any value. Just
pick a constant size for integers (4 bytes happens to be a very common one).
Or is it perfectly acceptable for the sizeof operator to different
results?


No, it''s not.

S.


In article <438cdcef.9338968@news-server>, oz****@bigpond.com (ozbear)
wrote:

If one were writing a C interpreter, is there anything in the standard
standard that requires the sizeof operator to yield the same value for
two different variables of the same type?

Let''s assume that the interpreter does conform to the range values
for, say, type int, but allocates storage for the variables based
on their value. So, for two variables foo and bar

int foo = 0; /* interpreter allocates two bytes */
int bar = 200000000; /* interpreter allocates four bytes */

Does the standard require that sizeof foo == sizeof bar thereby
making this allocation scheme broken, unless hidden in some way?
Or is it perfectly acceptable for the sizeof operator to different
results?



sizeof (foo) must be equal to sizeof (bar).

memcpy (&foo, &bar, sizeof (foo)) must have exactly the same effect as a
simple assignment foo = bar. And that assignment must work, so your
interpreter must change the memory allocated to foo from 2 byte to 4
byte.

If I write a function

void f (int* p, int value) { *p = value; }

then calling

f (&foo, 2000000000)

must work. Basically, everything must worked as guaranteed by the C
Standard. As long as the interpreter makes sure that everything works as
it should, it is free to do whatever it likes.


这篇关于C解释器和sizeof运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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