指针数组。如何检查索引没有失败? [英] Array of pointers. How to check index without failure?

查看:61
本文介绍了指针数组。如何检查索引没有失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个指向自定义对象的静态指针数组:MyObject *

aMyObjects [500]


我怎样才能检查索引有一个指针存储?假设没有

检查会导致崩溃,如果没有指针:aMyObjects [0] -

Hello, I have a static array of pointers to custom objects: MyObject *
aMyObjects[500]

How can i can check if an index has a pointer stored? Assuming without
checking leads to crashes if no pointer is there: aMyObjects[0]-


> stuff ... crash。
>stuff... crash.



任何对此的帮助都将非常感激。

Any help with this would be much appreciated.

推荐答案

Lorn写道:
Lorn wrote:

您好,我有一个指向自定义对象的静态数组指针:MyObject *

aMyObjects [500]


如何检查索引是否存储指针?假设没有

检查会导致崩溃,如果没有指针:aMyObjects [0] -
Hello, I have a static array of pointers to custom objects: MyObject *
aMyObjects[500]

How can i can check if an index has a pointer stored? Assuming without
checking leads to crashes if no pointer is there: aMyObjects[0]-

> stuff ... crash。
>stuff... crash.



如果对此有任何帮助将非常感激。


Any help with this would be much appreciated.



如果指针的值为null(0),那么它不会指向

任何东西。请注意,您需要初始化数组,以便那些不指向somthing的
元素设置为null。


T * x = 0;

....

如果(x)

{

x->东西......

}


If the value of the pointer is null (0) then it''s not pointing to
anything. Note that you need to initialize your array so that those
elements not pointing to somthing are set to null.

T * x = 0;
....
if ( x )
{
x->stuff ...
}


Lorn< ef ******* @ yahoo.comwrote:
Lorn <ef*******@yahoo.comwrote:

你好,我有一个指向自定义对象的静态数组指针:MyObject *

aMyObjects [500]


我怎样才能检查索引是否有指针存储?假设没有

检查会导致崩溃,如果没有指针:aMyObjects [0] -
Hello, I have a static array of pointers to custom objects: MyObject *
aMyObjects[500]

How can i can check if an index has a pointer stored? Assuming without
checking leads to crashes if no pointer is there: aMyObjects[0]-

stuff ... crash。
stuff... crash.



如果对此有任何帮助将非常感激。


Any help with this would be much appreciated.



将值设置为0或NULL。在

解除引用之前检查值是否为0.

Set the values to 0 or NULL. Check to see if the value is 0 before
dereferencing it.





Daniel T.写道:


Daniel T. wrote:

Lorn< ef ******* @ yahoo.comwrote:
Lorn <ef*******@yahoo.comwrote:

>您好,我有一个指向自定义对象的静态数组指针:MyObject *
aMyObjects [500]

我怎样才能检查索引是否存储了指针?假设没有
检查导致崩溃,如果没有指针:aMyObjects [0] -
>Hello, I have a static array of pointers to custom objects: MyObject *
aMyObjects[500]

How can i can check if an index has a pointer stored? Assuming without
checking leads to crashes if no pointer is there: aMyObjects[0]-

>> stuff ... crash。
>>stuff... crash.


任何对此的帮助都将非常感激。

Any help with this would be much appreciated.



将值设置为0或NULL。在

解除引用之前检查值是否为0。


Set the values to 0 or NULL. Check to see if the value is 0 before
dereferencing it.



为了防止悬空指针,更好的方法是使用

为每个指针设置一个额外的''初始化''位1/0适当。

To protect against dangling pointers, a better approach would be to use
an extra ''initialized'' bit for each pointer set to 1/0 appropriately.


这篇关于指针数组。如何检查索引没有失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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