I managed to re-create the problem and here it is:
It seems using a procedure with a variable untyped parameter and trying to
do a move with a dynamic array leads to access violation.
Why won't this work... ? what's up with move and dynamic arrays ?
// *** begin of test code ***:
procedure test( var something );
var
a : array[0..9] of byte;
begin
a[0] := 10;
a[1] := 20;
a[2] := 30;
a[3] := 40;
a[4] := 50;
a[5] := 60;
a[6] := 70;
a[7] := 80;
a[8] := 90;
a[9] := 100;
move( a, something, 10 );
end;
procedure TForm1.Button1Click(Sender: TObject);
var
b : array[0..9] of byte;
i : integer;
c : array of byte;
begin
{
test( b );
for i:=0 to 9 do
begin
ShowMessage( IntToStr( b[i] ) );
end;
// ok
}
SetLength( c, 10 );
test( c );
for i:=0 to 9 do
begin
ShowMessage( IntToStr( c[i] ) ); // access violation ?!??
end;
c := nil;
end;
// *** end of test code ***:
instead of passing c, just pass first element of c.
so
instead of passing dynamic array, pass first element of dynamic array :)
procedure test( var a );
begin
end;
var
c : array of byte;
begin
SetLength( c, 10 );
test( c[0] ); // this does work :)
c := nil;
end;
"Skybuck Flying" <nos...@hotmail.com> wrote in message
news:asd3g0$r1e$1...@news1.tilbu1.nb.home.nl...
pointer(c)^
The delphi doc however says not to do this :)
'Don't dereference dynamic array's' :)
Dynamic arrays are probably more than just pointers...
They probably have some reference counting which is before or behind the
pointer itself :)
"Skybuck Flying" <nos...@hotmail.com> wrote in message
news:asdu89$q8m$1...@news2.tilbu1.nb.home.nl...
No that's not true.
The C pointer is de-referenced with the ^ sign.
Pointer(c)^ points to the array itself.
> you use of the Pointer(C)^ is merly getting the contents of C which is a
> memory address...
Yes, the memory address is the address of the array itself.
This address is passed to the var parameter.
So what I am doing is passing the array itself.
Try this code:
ShowMessage('address of c^: ' + IntToStr(longint(c)) );
This shows where C points to.
ShowMessage('address of something: ' + IntToStr(longint(@something)) ); //
(inside test procedure)
This shows the address of something.
Which is clearly the same.
I am guessing var is just a temporarely variable on the stack which is
infact a pointer to the data.
Sometimes it's just a pointer to an integer or something else.
In this case it's just a pointer to the data of the array :)
I don't understand the assembler parts... :)