unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
type
  TShared = record
    Keys:array [0..5] of String;
  end;
  PShared = ^TShared;
var
  Form1: TForm1;
  Shared: PShared;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  Shared^.Keys[0]:='windows xp';
  Shared^.Keys[1]:='windows 2000';
  Shared^.Keys[2]:='windows 7';
  Shared^.Keys[3]:='windows 98';
  Shared^.Keys[4]:='windows 2003';
  Shared^.Keys[5]:='windows server 10';
end;end.click提示出错,但编译通过,错在哪?

解决方案 »

  1.   

    没有分配内存
    前面用new(Shared),后面记得释放
      

  2.   

    Shared是个指针,它所指向的实例没有创建,此时是不能访问它所指向的实例的。增加以下语句再测试:procedure TForm1.FormCreate(Sender: TObject);
    begin
      new(Shared);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      dispose(Shared);
    end;
      

  3.   

    需要初始化指针 New(Shared)
    dispose(shared)
      

  4.   


    procedure TForm1.Button1Click(Sender: TObject);
    begin
      New(Shared);
      Shared^.Keys[0]:='windows xp';
      Shared^.Keys[1]:='windows 2000';
      Shared^.Keys[2]:='windows 7';
      Shared^.Keys[3]:='windows 98';
      Shared^.Keys[4]:='windows 2003';
      Shared^.Keys[5]:='windows server 10';
      Dispose(Shared);
    end;