迭代器/指针算术 [英] Iterator/pointer arithmetic

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

问题描述

我有一个:


vector< A> V;


an


A * a;


可能指向一个元素V.


如何最快地检查这个?


我现在用的是


if(V.size()> 0&& a> =& V [0]&& a< =& V [V.size()]){it_is_in();对于我这个标准符合要求的
这个标准是否合适?

如果没有:我的应用程序永远不会在嵌入式系统上运行。

有没有人知道任何现有的标准符合编译器允许

说任何unix-workstation,windows system,mac甚至VMS

上面的代码会产生不是预期的?


谢谢,

marc

解决方案

< blockquote> Marc Schellens写道:

我有一个:

向量< A> V;

A * a;

这可能指向V的一个元素。

如何最快检查一下?

我现在用的是

if(V.size()> 0&& a> =& V [ 0]&& a< =& V [V.size()]){it_is_in();}

这对我来说很好,但是这个标准是否符合要求?
如果没有:我的应用程序将永远不会在嵌入式系统上运行。
是否有人知道任何现有的标准符合编译器让我们说任何unix-workstation,windows system,mac甚至VMS
上面的代码会不会产生预期的结果?

谢谢,
marc




指针算法取决于指针的属性。

所有指针都可以与NULL(==和!=)进行比较。其他

比较不保证,可能有意外行为。

指针可以通过加法或减法移动。任何其他

算术运算(例如乘法,除法,移位)

可能会导致意外行为。


在许多嵌入式系统中,指针转换为整数

并作为整数进行比较。这假设是一个平坦而线性的b $ b寻址空间。


-

Thomas Matthews

C ++新闻组欢迎辞:
http:/ /www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍




" Marc Schellens" < M _ ********* @ hotmail.com>在留言中写道

新闻:3F ************** @ hotmail.com ...

我有一个:

vector< A> V;

A * a;

这可能指向V的一个元素。

如何检查这个最快?




没有可移植的方法来确定一个完全未知的指针

是否指向特定对象。




" Marc Schellens" < M _ ********* @ hotmail.com> skrev i en meddelelse

新闻:3F ************** @ hotmail.com ...

我有一个:

vector< A> V;

A * a;

这可能指向V的一个元素。

如何最快检查一下?

我现在用的是

if(V.size()> 0&& a> =& V [ 0]&& a< =& V [V.size()]){it_is_in();}

这对我来说很好,但是这个标准是否符合要求?
如果没有:我的应用程序将永远不会在嵌入式系统上运行。
是否有人知道任何现有的标准符合编译器让我们说任何unix-workstation,windows system,mac甚至VMS
上面的代码会不会产生预期的结果?

谢谢,
marc




据我所知,这不可能做到以符合标准的方式。但是,如果你是b $ b,你定位一个已知的平台(比如Windows),如果它是调试代码的话,我不会担心它会是多少关于它的(它看起来非常就像它一样)。你忘记了b $ b,但要检查是否指向边界(例如V [0],V [1]或......)

而不是某些随机位置(a =(A *)V [0] .field)。这甚至需要

更脏的代码;-)

如果您的代码不是调试代码,我建议您重新考虑一下您的设计是否为
ok 。


亲切的问候

Peter


I have a:

vector<A> V;

an

A* a;

which might point to one element of V.

How to check this most quickly?

I am using for now something like

if( V.size() > 0 && a >= &V[0] && a <= &V[ V.size()] ) { it_is_in();}

which works fine for me, but is this standard conform?
And if not: My application will never run on an embedded system.
Does anybody know any existing standard conform compiler on lets
say any unix-workstation, windows system, mac or even VMS
where the above code would produce not the expected?

thanks,
marc

解决方案

Marc Schellens wrote:

I have a:

vector<A> V;

an

A* a;

which might point to one element of V.

How to check this most quickly?

I am using for now something like

if( V.size() > 0 && a >= &V[0] && a <= &V[ V.size()] ) { it_is_in();}

which works fine for me, but is this standard conform?
And if not: My application will never run on an embedded system.
Does anybody know any existing standard conform compiler on lets
say any unix-workstation, windows system, mac or even VMS
where the above code would produce not the expected?

thanks,
marc



Pointer arithmetic depends on the properties of the pointer.
All pointers can be compared to NULL (== and !=). Other
comparisons are not guaranteed and may have unexpected behavior.
A pointer can be moved via addition or subtraction. Any other
arithmetic operations (e.g. multiplication, division, shifting)
may induce unexpected behavior.

In many embedded systems, pointers are converted to integers
and compared as integers. This assumes a flat and linear
addressing space, though.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book



"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@hotmail.com...

I have a:

vector<A> V;

an

A* a;

which might point to one element of V.

How to check this most quickly?



There is no portable way to determine whether a completely unknown pointer
points at a particular object.



"Marc Schellens" <m_*********@hotmail.com> skrev i en meddelelse
news:3F**************@hotmail.com...

I have a:

vector<A> V;

an

A* a;

which might point to one element of V.

How to check this most quickly?

I am using for now something like

if( V.size() > 0 && a >= &V[0] && a <= &V[ V.size()] ) { it_is_in();}

which works fine for me, but is this standard conform?
And if not: My application will never run on an embedded system.
Does anybody know any existing standard conform compiler on lets
say any unix-workstation, windows system, mac or even VMS
where the above code would produce not the expected?

thanks,
marc



To my knowledge, this can not be done in a standards-conforming way. If,
however, You target a known platform (such as Windows), I would not worry to
much about it if it is debug-code (and it looks very much as it is). You
forgot, however to check if a points to a boundary (eg. at V[0], V[1] or...)
and not to some random place (a = (A*)V[0].field). This requires even
dirtier code ;-)
If your code is not debug-code, I suggest you reconsider if your design is
ok.

Kind regards
Peter


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

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