TC ++ PL中的矛盾? [英] contradiction in TC++PL?

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

问题描述

这是来自TC ++ PL Special Edition的代码片段,第291页:


void f1(T a)

{

T v [200];

T * p =& v [0];

p--; //请注意(我的评论)

* p = a; // oops:''p'超出范围,未被捕获

++ p;

* p = a; // ok

}


这里是p92的摘录:


"结果在初始元素之前获取元素的地址

未定义且应该避免。


作者说,据我所知,它'在p--之后可以使用++ p,我认为这可能与摘录中的陈述相矛盾。你觉得怎么样?


-

ES Kim

Here''s a code fragment from TC++PL Special Edition, p291:

void f1(T a)
{
T v[200];
T* p = &v[0];
p--; // please note (my comment)
*p = a; // oops: ''p'' out of range, uncaught
++p;
*p = a; // ok
}

And here''s an excerpt from p92:

"The result of taking the address of the element before the initial element
is undefined and should be avoided."

The author says, as I understand, it''s ok with ++p after p--, which I think
contradict the statement in the excerpt. What do you think?

--
ES Kim

推荐答案

嗨〜

很高兴见到你,你韩国人对吗?我要么..


无论如何,
Hi~
It''s good to see you, you Korean right? me either..

Anyway,
"在初始
元素之前获取元素地址的结果是未定义的,应该是避免"。
作者说,据我所知,p--之后的++ p是可以的,我认为这与摘录中的陈述相矛盾。你怎么看?


两个条款都是正确的恕我直言。

p--实际上返回第一个元素的参考,即使它减少了

内部指针..

这是因为那是'posfix ..

所以在这种情况下,没有办法在第0个之前访问该元素/>
元素..


然后''++ p''使指针指向第0个元素.. T v [200];
T * p =& v [0];
p--; //请注意(我的评论)
++ p;


那么,上面的代码并不危险..


这是你的问题吗?

:)


" ES Kim" < no@spam.mail>在消息中写道

news:cl ********** @ news1.kornet.net ...这是TC ++ PL Special Edition的代码片段,第291页:

void f1(T a)
{t v [200];
T * p =& v [0];
p-- ; //请注意(我的评论)
* p = a; // oops:''p'超出范围,未被捕获
++ p;
* p = a; // ok
}

这里是p92的摘录:

"在初始$ b之前获取元素地址的结果$ b元素是未定义的,应该避免使用。

作者说,据我所知,在p--之后可以使用++ p,我认为这是相反的摘录中的陈述。您怎么看?

-
ES Kim
"The result of taking the address of the element before the initial element is undefined and should be avoided." The author says, as I understand, it''s ok with ++p after p--, which I think contradict the statement in the excerpt. What do you think?
Both clauses are right IMHO.
p-- actually returns the fist element''s reference even though it decreaments
internal pointer..
It''s because that''s posfix..
So in this case, there in no way to access the element before the zeroth
element..

And then ''++p'' makes the pointer point to the zeroth element.. T v[200];
T* p = &v[0];
p--; // please note (my comment)
++p;
So then, the code above is not dangerous..

Is that your questions?
:)

"ES Kim" <no@spam.mail> wrote in message
news:cl**********@news1.kornet.net... Here''s a code fragment from TC++PL Special Edition, p291:

void f1(T a)
{
T v[200];
T* p = &v[0];
p--; // please note (my comment)
*p = a; // oops: ''p'' out of range, uncaught
++p;
*p = a; // ok
}

And here''s an excerpt from p92:

"The result of taking the address of the element before the initial element is undefined and should be avoided."

The author says, as I understand, it''s ok with ++p after p--, which I think contradict the statement in the excerpt. What do you think?

--
ES Kim



" BekTek" <是**** @ gmail.com>在消息中写道

news:h8_cd.2969
"BekTek" <be****@gmail.com> wrote in message
news:h8_cd.2969


7d7.1176@trnddc04 ...
7d7.1176@trnddc04...
嗨〜
它很高兴见到你,你韩国人对吗?我要么..


是的,我是韩国人。很高兴见到你。

无论如何,
Hi~
It''s good to see you, you Korean right? me either..
Yes, I''m a Korean. Nice to see you.

Anyway,
"在初始


元素

是未定义的,应该避免使用。
is undefined and should be avoided."


作者说,据我所知,在p--之后可以使用++ p ,我
The author says, as I understand, it''s ok with ++p after p--, which I


认为

与摘录中的陈述相矛盾。你觉得怎么样?
contradict the statement in the excerpt. What do you think?



两个条款都是正确的恕我直言。
p--实际上返回了第一个元素的引用,即使它减少了内部指针。 。
这是因为那是'posfix ..
所以在这种情况下,没有办法在第0个元素之前访问元素..



Both clauses are right IMHO.
p-- actually returns the fist element''s reference even though it decreaments
internal pointer..
It''s because that''s posfix..
So in this case, there in no way to access the element before the zeroth
element..

And then ''++p'' makes the pointer point to the zeroth element..




我不确定你的意思是什么''内部指针''。

在此上下文中,后缀和前缀形式没有区别。

表达式p--被计算为指向初始元素的指针,

但它也有副作用,这使得p指向

之前的地址为初始元素。


-

ES Kim



I''m not sure what you mean by ''internal pointer''.
There''s no difference between postfix and prefix form in this context.
The expression p-- is evaluated as a pointer to the initial element,
but it also has a side effect, which makes p point to the address before
the initial element.

--
ES Kim


这篇关于TC ++ PL中的矛盾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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