问题描述
您好,我有一个指向自定义对象的静态指针数组: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]-
任何对此的帮助都将非常感激。
Any help with this would be much appreciated.
推荐答案
如果对此有任何帮助将非常感激。
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 ...
}
如果对此有任何帮助将非常感激。
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.
任何对此的帮助都将非常感激。
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.
这篇关于指针数组。如何检查索引没有失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!