wrong logic here:
    FileRecord := Pointer(FileList.Objects[FileCnt]);
    Files[FileCnt] := FileRecord^;
 
 
e.g.
 
an array of :
 
  0   a
  1   d
    2      c
    3      b
    4      f
    5     e
 
after sorted :
   0      a
   3      b
   2    c
 1   d 
  5       e
  4       f
 
What the two lines code do is :
 0 = 0 = a
 1 = 3 = b         // note here: element 1 (d) is replaced by b
 2 = 2 = c 
until now it is fine.
then it goes wrong:
3 = 1 = b         // expect it to be d but it is replaced by b as above
 
 
solution :
define another array of  TFileRec :
 
    FileRecord := Pointer(FileList.Objects[FileCnt]);
    AnotherFiles[FileCnt] := FileRecord^;
 
 
 
Have a good day. 
 
 
 
 
 

-----Original Message-----
From: Ross Levis [mailto:[EMAIL PROTECTED]
Sent: Wednesday, 26 May 2004 9:24 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] dynamic array items

A quick question (I think).  Does each item of a dynamic array use sequential memory space or can each item be in a completely different memory location?  The reason I ask is I want to sort an array of records (don't ask) and the quickest way would be to swap pointer locations for each item, rather than move record values themselves.
 
As I see it, it depends on whether SetLength moves the whole array to another memory location as I believe it does with a dynamic string.
 
I am trying to use a TStringList to do the sorting and store the address of each array element in the Objects property, but either it can't be done or I'm doing something wrong because the records are ending up blank.
 
Files is an array of FileRec (a record structure).  PFileRec = ^FileRec;
 
var
  FileCnt: Cardinal;
  FileList: TStringList;
  FileRecord: PFileRec;
begin
  FileList := TStringList.Create;
  for FileCnt := 0 to High(Files) do
      FileList.AddObject(ExtractFilename(Files[FileCnt].Filename),@Files[FileCnt]);
  FileList.Sort;
  for FileCnt := 0 to High(Files) do
  begin
    FileRecord := Pointer(FileList.Objects[FileCnt]);
    Files[FileCnt] := FileRecord^;
  end;
  FileList.Free;
end;
 
Cheers,
Ross Levis.
_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to