在 records/pointers 上使用 .all 时如何防止访问检查失败?
How to prevent an access check failed when using .all on records/pointers?
这是设置我的所有指针和记录等内容的代码。
type BSTNode;
type BSTNodePtr is access BSTNode;
type BSTNode is record
key: Key_Type;
data: Item_Type;
left, right: BSTNodePtr;
end record;
type BSTree is record
root: BSTNodePtr;
end record;
我有一个功能是:
function contains(key: Key_Type; t: BSTree) return Boolean is
temp_node : BSTNodePtr := t.root;
right : boolean;
left : boolean;
temp_tree : BSTree;
begin
if temp_node.all.key = key then --error occurs
return true;
elsif temp_node.all.left /= null and
temp_node.all.right /= null then
temp_tree.root := temp_node.all.left;
left := contains(key, temp_tree);
temp_tree.root := temp_node.all.right;
right := contains(key, temp_tree);
if left = true or right = true then
return true;
else
return false;
end if;
else
return false;
end if;
end contains;
每次尝试执行 .all 时,都会出现约束错误,访问检查失败。我知道这是因为它不知道该代码是否已分配,但我不知道我需要做什么才能访问它或让它在不给出错误的情况下访问它。如果有人可以提供帮助或知道我做错了什么,将不胜感激。谢谢!
我想我明白了。在我可以使用 .access 之前,我需要先检查它以确保它不为空。所以,我只是在大部分代码之前添加了一个 if 语句。这是有道理的,我知道为什么我花了这么长时间才想到哈哈。
很高兴听到您能够找到解决方案!提示:您还可以拆分函数并将代码简化为:
function Contains (Key : Key_Type; N : BSTNode) return Boolean is
begin
return (N.Key = Key)
or else ((N.Left /= null) and then Contains (Key, N.Left.all))
or else ((N.Right /= null) and then Contains (Key, N.Right.all));
end Contains;
function Contains (Key : Key_Type; T : BSTree) return Boolean is
begin
return (T.Root /= null) and then Contains (Key, T.Root.all);
end Contains;
此处,short-circuit binary operator and then
的行为用于防止指针的解除引用,如果它是 null
。
这是设置我的所有指针和记录等内容的代码。
type BSTNode;
type BSTNodePtr is access BSTNode;
type BSTNode is record
key: Key_Type;
data: Item_Type;
left, right: BSTNodePtr;
end record;
type BSTree is record
root: BSTNodePtr;
end record;
我有一个功能是:
function contains(key: Key_Type; t: BSTree) return Boolean is
temp_node : BSTNodePtr := t.root;
right : boolean;
left : boolean;
temp_tree : BSTree;
begin
if temp_node.all.key = key then --error occurs
return true;
elsif temp_node.all.left /= null and
temp_node.all.right /= null then
temp_tree.root := temp_node.all.left;
left := contains(key, temp_tree);
temp_tree.root := temp_node.all.right;
right := contains(key, temp_tree);
if left = true or right = true then
return true;
else
return false;
end if;
else
return false;
end if;
end contains;
每次尝试执行 .all 时,都会出现约束错误,访问检查失败。我知道这是因为它不知道该代码是否已分配,但我不知道我需要做什么才能访问它或让它在不给出错误的情况下访问它。如果有人可以提供帮助或知道我做错了什么,将不胜感激。谢谢!
我想我明白了。在我可以使用 .access 之前,我需要先检查它以确保它不为空。所以,我只是在大部分代码之前添加了一个 if 语句。这是有道理的,我知道为什么我花了这么长时间才想到哈哈。
很高兴听到您能够找到解决方案!提示:您还可以拆分函数并将代码简化为:
function Contains (Key : Key_Type; N : BSTNode) return Boolean is
begin
return (N.Key = Key)
or else ((N.Left /= null) and then Contains (Key, N.Left.all))
or else ((N.Right /= null) and then Contains (Key, N.Right.all));
end Contains;
function Contains (Key : Key_Type; T : BSTree) return Boolean is
begin
return (T.Root /= null) and then Contains (Key, T.Root.all);
end Contains;
此处,short-circuit binary operator and then
的行为用于防止指针的解除引用,如果它是 null
。