我如何初始化此代码?
type
PPNode = ^PNode;
PNode = ^Node;
CNode = array of PPNode;
Node = record
key: Integer;
next: PNode;
prev: PNode;
end;
我用这种方式:
function TForm1.chained_hash_init(n: Integer): CNode;
var
A: Cnode;
begin
...
SetLength(A, N);
Result := A;
...
end;
但是对于此访问,我的内存有错误:
procedure TForm1.btn1Click(Sender: TObject);
var
pcnArr: CNode;
begin
SetLength(pcnArr, 19);
pcnArr := chained_hash_init(19);
ShowMessage( IntToStr(pcnArr[i]^^.key)) ); // I have Problem Here :(
end;
如何初始化Cnode?
最佳答案
您没有在 ShowMesage 之前初始化 pcnArr[i]
。所以你会得到“访问冲突”错误。
因此您应该初始化 pcnArr
例如:
function TForm1.chained_hash_init(n: Integer): CNode;
var
A: Cnode;
i:integer;
P:PNode;
begin
...
SetLength(A, N);
for i:=0 to N-1 do
begin
new(A[i]);
new(A[i]^);
with A[i]^^ do
begin
key:=0;
next:=nil;
prev:=nil;
end;
end;
Result := A;
...
end;
关于delphi - Delphi中指向指针的初始数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211138/