另一个问题 [英] Another sizeof question

查看:82
本文介绍了另一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果已经报道,请原谅。


给予


char x [42];





sizeof(x [999])


任何类型的错误?如果是这样,既然没有评估表达式,那么如何检测到这样的错误?b
如果声明是


int n = 42;

char x [n];





- Richard

-

"考虑到需要多达32个字符某些字母表中的
" - 1963年的X3.4。

Please excuse me if this has already been covered.

Given

char x[42];

is

sizeof(x[999])

any kind of error? If so, since the expression is not evaluated, how
would such an error be detected? What if the declaration was

int n = 42;
char x[n];

?

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

推荐答案

ri ***** @ cogsci.ed.ac.uk (Richard Tobin)写道:
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:

如果已经涵盖,请原谅我。


给定


char x [42];





sizeof(x [999])


任何类型的错误?
Please excuse me if this has already been covered.

Given

char x[42];

is

sizeof(x[999])

any kind of error?



[...]


我相信它完全有效,而且必须收益1.


x [999]相当于*(x + 999)。添加会调用未定义的

行为,但前提是它已被评估。


我认为没有更多理由

sizeof( x [999])

调用UB而不是

if(0){

x [999];

}

这样做。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< * < http://users.sdsc.edu/~kst>

"我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

[...]

I believe it''s perfectly valid, and must yield 1.

x[999] is equivalent to *(x+999). The addition would invoke undefined
behavior, but only if it were evaluated.

I see no more reason for
sizeof(x[999])
to invoke UB than for
if (0) {
x[999];
}
to do so.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


9月19日,3日:09:00,Keith Thompson< ks ... @ mib.orgwrote:
On Sep 19, 3:09 am, Keith Thompson <ks...@mib.orgwrote:

我相信它完全有效,而且必须产生1个。


x [999]相当于*(x + 999)。添加会调用未定义的

行为,但前提是它已被评估。


我认为没有更多理由

sizeof( x [999])

调用UB而不是

if(0){

x [999];

}

这样做。
I believe it''s perfectly valid, and must yield 1.

x[999] is equivalent to *(x+999). The addition would invoke undefined
behavior, but only if it were evaluated.

I see no more reason for
sizeof(x[999])
to invoke UB than for
if (0) {
x[999];
}
to do so.



真的一定很好。类似的情况就是这个,可以在每个人的代码中找到数以万计的时间::


某事* p;

p = malloc(sizeof(* p));


这是建议的分配内存的习惯用法,以及表达式

* p本身将像x [999]一样调用未定义的行为。这是好b $ b罚款,因为* p未被评估。

It really must be fine. A similar situation is this one, which can be
found gazillion times in everyone''s code::

something* p;
p = malloc (sizeof (*p));

This is the recommended idiom to allocate memory, and the expression
*p on its own would invoke undefined behaviour just like x[999]. It''s
fine because *p is not evaluated.


2007年9月19日星期三00:16:49 +0000,Richard托宾写道:
On Wed, 19 Sep 2007 00:16:49 +0000, Richard Tobin wrote:

如果已经报道,请原谅我。


鉴于


char x [42];





sizeof(x [999])


任何类型的错误?如果是这样,既然没有评估表达式,那么如何检测到这样的错误?b
如果声明是


int n = 42;

char x [n];
Please excuse me if this has already been covered.

Given

char x[42];

is

sizeof(x[999])

any kind of error? If so, since the expression is not evaluated, how
would such an error be detected? What if the declaration was

int n = 42;
char x[n];



即使在这种情况下,x'的类型是VLA,但x [999]的类型是char,

因此它不会被sizeof评估。


-

Army1987(将NOSPAM替换为email)

如果你是从Windows机器发送电子邮件,请关闭微软的

傻瓜??智能行情?特征。这样你就可以避免通过你的邮件洒垃圾
字符了。 - Eric S. Raymond和Rick Moen

Even in this case, x''s type is a VLA, but x[999]''s type is char,
so it is not evaluated by sizeof.

--
Army1987 (Replace "NOSPAM" with "email")
If you''re sending e-mail from a Windows machine, turn off Microsoft''s
stupid a??Smart Quotesa?? feature. This is so you''ll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen


这篇关于另一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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