You are not logged in.
Pages: 1
Thanks, but that doesn't seem to have fixed the problem.
I'm thinking that the Delphi 2007 compiler has bugs when it comes to returning dynamic arrays from recursive functions.
I saw something like this happen:
function SomeFunc: TSomeArray;
begin
// ... <-------------
result := SomeFunc;
end;
(That's plain Delphi code, nothing to do with TDynArray.)
At the indicated line, result retained its value from the previous iteration. It wasn't initialized. I think that a reference-counted local variable should be automatically initialized by the compiler. The fact that it wasn't suggests that the compiler has problems dealing with that case.
If I change the code as follows, then it works as expected. The changed lines are the ones marked.
function recurse(i: integer): TRecArray;
var
dyn: TDynArray;
rec: TRec;
childVals: TRecArray;
begin
dyn.Init(TypeInfo(TRecArray), result);
rec.Val1 := Format('This is Val1: %d', [i]);
rec.Val2 := Format('This is Val2: %d', [i]);
dyn.Add(rec);
if i > 0 then
begin
childVals := recurse(i - 1);
//dyn.AddArray(childVals); // <-------
for rec in childVals do // <-------
dyn.Add(rec); // <-------
end;
end;
Merci. TDynArray est une classe très sympa. Elle va vraiment me simplifier la vie. Je vous ai donné un petit don.
Consider this program:
program Project49;
{$APPTYPE CONSOLE}
uses
SysUtils, SynCommons;
type
TRec = packed record
val1: string;
val2: string;
end;
TRecArray = array of TRec;
function recurse(i: integer): TRecArray;
var
dyn: TDynArray;
rec: TRec;
childVals: TRecArray;
begin
dyn.Init(TypeInfo(TRecArray), result);
rec.Val1 := Format('This is Val1: %d', [i]);
rec.Val2 := Format('This is Val2: %d', [i]);
dyn.Add(rec);
if i > 0 then
begin
childVals := recurse(i - 1);
dyn.AddArray(childVals);
end;
end;
var
recs: TRecArray;
rec: TRec;
begin
recs := recurse(10);
for rec in recs do
begin
WriteLn(rec.Val1);
WriteLn(rec.Val2);
end;
end.
I would expect to see this output:
This is Val1: 10
This is Val2: 10
This is Val1: 9
This is Val2: 9
This is Val1: 8
This is Val2: 8
This is Val1: 7
This is Val2: 7
This is Val1: 6
This is Val2: 6
This is Val1: 5
This is Val2: 5
This is Val1: 4
This is Val2: 4
This is Val1: 3
This is Val2: 3
This is Val1: 2
This is Val2: 2
This is Val1: 1
This is Val2: 1
This is Val1: 0
This is Val2: 0
But instead I see this:
This is Val1: 10
This is Val2: 10
This is Val1: 9
This is Val2: 9
This is Val1: 8
This is Val2: 8
This is Val1: 7
This is Val2: 7
This is Val1: 6
This is Val2: 6
This is Val1: 5
This is Val2: 5
(There are some blank lines at the end, but FluxBB has chopped them.) The last 5 records in the array contain empty strings. Is this a memory corruption bug?
I am using Delphi 2007, and the latest version of SynCommons.
Hello
If I want to get TDynArray, is it better to download mORMot and grab the files out of there, or is it better to download the files individually?
If I try to download synopse.inc, I get a "403 Fobidden" error: http://synopse.info/fossil/raw/Synopse. … 29dc18f480
Pages: 1