delphi - Delphi中指向指针的初始数组

标签 delphi pointers hashtable

我如何初始化此代码?

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/

相关文章:

delphi - 如何以编程方式将图像插入Microsoft Word?

delphi - TXMLDocument 的替代方案?

c++ - 检查对象是否是基于基类的子类的实例

c++ - c++中不同指针语法之间的语义差异?

C++ 指针概念

powershell - "The LinkedList node does not belong to current LinkedList"

performance - 填充非常大的哈希表-如何最有效地做到这一点?

delphi - 虚拟字符串树 - 当父节点隐藏时显示子节点

delphi - 通过Delphi的TIdHTTP访问Clickbank API?

Perl 打印哈希数组的数组?