so3
asked on
Copy dynamic array
I'm trying to make a procedure that copies a dynamic array in another but after running the procedure destination array is still the sam. How can i make it so when quiting the procedure the content of array will be the good one.
Here is the procedure i use:
Procedure RebuildFilesArray(OldFiles : array of TFiles; NrFiles:Integer);
var i,nr_new:integer;
var NewFiles: array of TFiles;
begin
nr_new:=0;
for I := 0 to NrFiles-1 do
begin
if (OldFiles[i].Url<>'') and (OldFiles[i].Title<>'') then
begin
SetLength(NewFiles,nr_new+ 1);
NewFiles[nr_new]:=OldFiles [i];
Inc(nr_new);
end;
end;
for I := 0 to nr_new - 1 do
begin
OldFiles[i]:=NewFiles[i];
end;
NewSizeArray:=nr_new;
end;
here is the code that i want to give to the new array
NewSizeArray:=Length(baza[ index].Fil es);
RebuildFilesArray(baza[ind ex].Files, NewSizeArr ay);
SetLength(baza[index].File s,NewSizeA rray);
After running the procedure baza[index].Files will have the same content as before.
Here is the procedure i use:
Procedure RebuildFilesArray(OldFiles
var i,nr_new:integer;
var NewFiles: array of TFiles;
begin
nr_new:=0;
for I := 0 to NrFiles-1 do
begin
if (OldFiles[i].Url<>'') and (OldFiles[i].Title<>'') then
begin
SetLength(NewFiles,nr_new+
NewFiles[nr_new]:=OldFiles
Inc(nr_new);
end;
end;
for I := 0 to nr_new - 1 do
begin
OldFiles[i]:=NewFiles[i];
end;
NewSizeArray:=nr_new;
end;
here is the code that i want to give to the new array
NewSizeArray:=Length(baza[
RebuildFilesArray(baza[ind
SetLength(baza[index].File
After running the procedure baza[index].Files will have the same content as before.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
thanks,
i'll try when i arrive at home.
ziolko,
how can i use move(). Can you give me an example please?
i'll try when i arrive at home.
ziolko,
how can i use move(). Can you give me an example please?
Move(OldFiles[0],NewFiles[ 0],nr_new) ;
one more tip if TFiles is class then write descendatnt of TObjectList and add new method Rebuild() which will
remove from list items which .Url or .List is empty string.
ziolko.
one more tip if TFiles is class then write descendatnt of TObjectList and add new method Rebuild() which will
remove from list items which .Url or .List is empty string.
ziolko.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ZhaawZ indeed :)
ziolko.
ziolko.
ASKER
SetLength(OldFIles,0);
SetLength(OldFiles,nr_new) ;
When i tried to use setlength in procedure for oldfiles i get errors like incompatible types. Outside works ok
I've also tried Move(NewFiles,OldFiles,len gth(NewFil es)*SizeOf (tfiles)) but is not working. I get a memory error after running the code and also it puts in oldfiles array characters as in a strem #0#0...
I try to copy to content of newfiles in oldfiles and after running the procedure to still remain in oldfiles. did i used move correctly?
Also i've tried to use this but after using i get errors in memory:
for I := 0 to nr_new - 1 do
Move(NewFiles[i],baza[inde x].Files[i ],sizeof(T files));
if i use the code outside the procedure - that i've post it in my question everything is ok
SetLength(OldFiles,nr_new)
When i tried to use setlength in procedure for oldfiles i get errors like incompatible types. Outside works ok
I've also tried Move(NewFiles,OldFiles,len
I try to copy to content of newfiles in oldfiles and after running the procedure to still remain in oldfiles. did i used move correctly?
Also i've tried to use this but after using i get errors in memory:
for I := 0 to nr_new - 1 do
Move(NewFiles[i],baza[inde
if i use the code outside the procedure - that i've post it in my question everything is ok
have You tried my solution?
ASKER
yes i've tried your suggestion to but still when exting the content of tfiles is the same
hello so3 , , is there are long strings in your record in your dynamic array then , I have never been able to use the Move or CopyMemory functions to transfer a string from one array pos to another
( I beleive the delphi memory manager keeps track of "Where" every long string is allocated, so it can "Free" the string when it goes out of scope, so when you reduce the length of your dynamic array, it can free the string memory in the array records)
below is code for a button click, to "Compact" an array and Remove empty strings
procedure TForm1.sbut_ReArrayClick(S ender: TObject);
type
TFiles = record
Url, Title: String;
end;
var
theFiles: array of TFiles;
i, re, empty1: Integer;
begin
SetLength(theFiles, 16);
for i := 0 to High(theFiles) do
begin
if i in[4, 6, 9] then Continue;
theFiles[i].Url := 'www.addy'+IntToStr(i)+'.com';
theFiles[i].Title := 'Web Site '+IntToStr(i);
end;
// the code above just fills the array, is NOT for compacting the array
ShowMessage('Len '+IntToStr(Length(theFiles ))+' |'+theFiles[6].Url);
// showmessage for empty string
// code below removes the empty strings from theFiles
empty1 := 0;
for i := 0 to High(theFiles) do
if (theFiles[i].Url = '') and (theFiles[i].Title = '') then
begin
for re := i to High(theFiles)-empty1-1 do
theFiles[re] := theFiles[re+1];
Inc(empty1);
end;
SetLength(theFiles, Length(theFiles)-empty1);
// code below is to see result
ShowMessage('Len '+IntToStr(Length(theFiles ))+' |'+theFiles[6].Url);
end;
( I beleive the delphi memory manager keeps track of "Where" every long string is allocated, so it can "Free" the string when it goes out of scope, so when you reduce the length of your dynamic array, it can free the string memory in the array records)
below is code for a button click, to "Compact" an array and Remove empty strings
procedure TForm1.sbut_ReArrayClick(S
type
TFiles = record
Url, Title: String;
end;
var
theFiles: array of TFiles;
i, re, empty1: Integer;
begin
SetLength(theFiles, 16);
for i := 0 to High(theFiles) do
begin
if i in[4, 6, 9] then Continue;
theFiles[i].Url := 'www.addy'+IntToStr(i)+'.com';
theFiles[i].Title := 'Web Site '+IntToStr(i);
end;
// the code above just fills the array, is NOT for compacting the array
ShowMessage('Len '+IntToStr(Length(theFiles
// showmessage for empty string
// code below removes the empty strings from theFiles
empty1 := 0;
for i := 0 to High(theFiles) do
if (theFiles[i].Url = '') and (theFiles[i].Title = '') then
begin
for re := i to High(theFiles)-empty1-1 do
theFiles[re] := theFiles[re+1];
Inc(empty1);
end;
SetLength(theFiles, Length(theFiles)-empty1);
// code below is to see result
ShowMessage('Len '+IntToStr(Length(theFiles
end;
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I counted on my fingers, instead of in my head, and it should be -
for re := i to High(aryFiles)-empty1-2 do
sorry,
for re := i to High(aryFiles)-empty1-2 do
sorry,
ASKER
Thanks Slick812 but this code is what i used and is very slow, that's why i want to use another array of tfiles and copy it back because is very fast this way. But anyway i fixed this problem.
Procedure RebuildFilesArray( VAR OldFiles: array of TFiles; NrFiles:Integer);