声明   TObjLine = record
       Numero: integer;
       StartPointX:Integer;
       StartPointY:integer;
       EndPointX:Integer;
       EndPointY:integer;
    end;
var ObjTest: array of TObjLine;
调用:
procedure TForm1.FormCreate(Sender: TObject);
begin
  i := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  inc(i);
  SetLength(ObjTest, i);//动态设置数组长度
  ObjTest[i].Numero := i;
  ObjTEst[i].StartPointX:=(Random(ImageArea.Width));
  ObjTEst[i].StartPointY:=(Random(ImageArea.Width));
  objtest[i].EndPointX := (Random(ImageArea.Width));
  objtest[i].EndPointY := (Random(ImageArea.Width));
end;为什么在调用几次,就出现地址错误

解决方案 »

  1.   

    我试了在delphi7下调用也不行,为什么,什么地方有错误,帮忙呀
      

  2.   

    所谓动态,是在指声明使用时指定大小,(数组在内存里面被分配的是连续的地址空间)而你却在每个click事件里面都改变它,就可能出现所需的连续空间不够用的时候,所以就地址错误了
      

  3.   

    inc(i);
    objtest:=nil;//加上它试试看
    SetLength(ObjTest, i);//动态设置数组长度
      

  4.   

    a:array of Ti;
    setlength(a,Max)
    拜托,动态数组的的下标范围是0..Max-1
      

  5.   

    动态数组的下标总是从0到i-1的,你用low试试就知道了.对于动态数组,delphi是不负责下标越界检查的.唉,前几天看乔林的<参透delphi/kylix>还觉得这个人怎么这么罗嗦,老是提醒说程序员要小心下标越界,老是说这是程序员的责任....
      

  6.   

    个人觉得用动态数组不如用 TList 更好!    TObjLine = class
           Numero: integer;
           StartPointX:Integer;
           StartPointY:integer;
           EndPointX:Integer;
           EndPointY:integer;
        end;    TObjLineList = class
        
        ...
       
        public
          function AddObjLine: TObjLine;
          property CurObjLine: TObjLine ...
          function FindObjLine: TObjLine;
          procedure First;
          procedure Next;
          procedure Prior;
          procedure Last;
          procedure ClearObjLine;
          procedure DeleteObjLine(Idx: Integer); override
          procedure DeleteObjLine(Item: TObjLine); override
          ...
        end;   这样你可以向操作 DataSet中的Record一样处理对象列表
       ...
      

  7.   

    忘了要在TObjLineList 内部定义
      FObjLineList: TList
      在Create 时创建 FObjLineList 实例
      Destroy 时销毁 FObjLineList 实例
      并且注意 Clear 与 Delete 方法  不要造成内存泄漏!!!