指针算术问题 [英] Pointer arithmetic questions

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

问题描述

以下代码符合ISO C ++标准吗?

如果是,是否保证它不会在兼容平台上崩溃?

如果是,它会打印吗? ;指针是相等的在任何兼容的平台上?

如果p指向本地内存或字符串文字,答案是否相同?

char * p = new char [10];

char * p1 = p-1;

p1 = p1 + 1;


if(p1 == p)

{

cout<< 指针是相等的 <<结束;

}


谢谢,

Michael

Is the following code ISO C++ standard compliant?
If yes, is it guaranteed that it will not crash on compliant platforms?
If yes, will it print "Pointers are equal" on any compliant platform?
Will answers be the same if p points to local memory or string literal?
char *p = new char[10];
char *p1 = p-1;
p1 = p1 + 1;

if(p1 == p)
{
cout << "Pointers are equal" << endl;
}

Thank you,

Michael

推荐答案

MO******@gmail.com
以下代码符合ISO C ++标准吗?
如果是,是否保证它不会在兼容平台上崩溃?
如果是,它会不会打印指针是相等的在任何兼容的平台上?
如果p指向本地内存或字符串文字,答案是否相同?

char * p = new char [10];
char * p1 = p-1;
p1 = p1 + 1;

if(p1 == p)
{
cout<< 指针是相等的 <<结束;


谢谢你,

迈克尔
Is the following code ISO C++ standard compliant?
If yes, is it guaranteed that it will not crash on compliant platforms?
If yes, will it print "Pointers are equal" on any compliant platform?
Will answers be the same if p points to local memory or string literal?
char *p = new char[10];
char *p1 = p-1;
p1 = p1 + 1;

if(p1 == p)
{
cout << "Pointers are equal" << endl;
}

Thank you,

Michael




我会说这是符合标准的(没有查找)。那就是说,在递增之前使用p1的
是未定义的(显然)。


如果我错了,有人会纠正我,但所有已经完成的工作是p1在p之前设置为

1 * sizeof(char)。我说1 * sizeof(char)因为它取决于系统上char的

大小。对于大多数系统,sizeof(char)为1;所以p1是

在p之前初始化为1个字节。如果你使用int而不是char,那么

将是4个字节(在我的32位机器上)p。


但你不需要真的关心这些细节,那就是指针算术的美丽!


-

Alvin



I would say this is standard compliant (without looking it up). That said,
using p1 before incrementing is undefined (obviously).

Someone correct me if I''m wrong but all that has been done is p1 is set to
1*sizeof(char) before p. I say 1*sizeof(char) because it depends on the
size of a char on the system. For most systems, sizeof(char) is 1; so p1 is
intialised to 1 byte before p. If you use int instead of char, then it
would be 4 bytes (on my 32bit machine) before p.

But you don''t need to be concerned with the particulars really, That''s the
beauty of pointer arithmetic!

--
Alvin


Alvin写道:

我想说这是符合标准的(没有查找)。


然后你最好仔细查看。 < g取代;指针算术的规则是

,你可以在一个数组内移动或者移动到

结束之后的一个位置。你不能放在开头。

对于大多数系统,sizeof(char)是1;

I would say this is standard compliant (without looking it up).
Then you''d better look it up. <g> The rule for pointer arithmetic is
that you can move around within an array or to one position past the
end. You can''t go in front of the beginning.

For most systems, sizeof(char) is 1;




sizeof(char )定义为1.


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Pete Becker写道:
Pete Becker wrote:
Alvin写道:

我想说这是符合标准的(没有查找)。

I would say this is standard compliant (without looking it up).



然后你最好仔细查看。 < g取代;指针运算的规则是,你可以在一个数组内移动或移动到
端的一个位置。你不能放在开头。



Then you''d better look it up. <g> The rule for pointer arithmetic is
that you can move around within an array or to one position past the
end. You can''t go in front of the beginning.


对于大多数系统,sizeof(char)是1;

For most systems, sizeof(char) is 1;


sizeof(char)定义为1.



sizeof(char) is defined to be 1.




我想我并没有说清楚。按照标准符合我试图

说p1 = p-1是合规的。我并不打算说结果是符合



我猜你所说的是走在阵列前面的行为是

undefined。


-

Alvin



I guess I didn''t make myself clear. By standard compliant I was trying to
say p1 = p-1 was compliant. I didn''t intend on saying the result was
compliant.

I guess what you are saying is the act of going in front of the array is
undefined.

--
Alvin


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

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