Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-12

Using Move() with dynamic arrays -> RunError(0)

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Žilvinas Ledas

Hello all,

I'm trying to use Move() with dynamic arrays, but don't know why it fails...
When instead commented out "for .. do" code I use Move() code my app gets RunError(0) after some time. When I use "for .. do" everything works ok.
I'm using Free Pascal Compiler version 2.3.1 [2009/11/03] for i386 on Windows 7.
What can be the problem here?

type
  TArrayOfSmallInt = array of SmallInt;
function TFrame.getAllFrame(): TArrayOfSmallInt;
var
  tmpBuf: TArrayOfSmallInt;
  i: Integer;
begin
  startOverlapLen := Length(startOverlap);
  mainAreaLen := Length(mainArea);
  endOverlapLen := Length(endOverlap);
  SetLength(tmpBuf, startOverlapLen+mainAreaLen+endOverlapLen);
{
  for i := 0 to startOverlapLen-1 do
    tmpBuf[i] := startOverlap[i];
  for i := 0 to mainAreaLen-1 do
    tmpBuf[i+startOverlapLen] := mainArea[i];
  for i := 0 to endOverlapLen-1 do
    tmpBuf[i+startOverlapLen+mainAreaLen] := endOverlap[i];
}
  Move(startOverlap[0], tmpBuf[0], SizeOf(SmallInt)*startOverlapLen);
  Move(mainArea[0], tmpBuf[startOverlapLen], SizeOf(SmallInt)*mainAreaLen);
  Move(endOverlap[0], tmpBuf[startOverlapLen+mainAreaLen],
SizeOf(SmallInt)*endOverlapLen);
   Result := tmpBuf;
end;

Regards,
Žilvinas

Marco van de Voort
to Žilvinas Ledas

In our previous episode, Žilvinas Ledas said:

Show quoted text 31 lines
function TFrame.getAllFrame(): TArrayOfSmallInt; var tmpBuf: TArrayOfSmallInt; i: Integer; begin startOverlapLen := Length(startOverlap); mainAreaLen := Length(mainArea); endOverlapLen := Length(endOverlap); SetLength(tmpBuf, startOverlapLen+mainAreaLen+endOverlapLen); { for i := 0 to startOverlapLen-1 do tmpBuf[i] := startOverlap[i]; for i := 0 to mainAreaLen-1 do tmpBuf[i+startOverlapLen] := mainArea[i]; for i := 0 to endOverlapLen-1 do tmpBuf[i+startOverlapLen+mainAreaLen] := endOverlap[i]; } Move(startOverlap[0], tmpBuf[0], SizeOf(SmallInt)*startOverlapLen); Move(mainArea[0], tmpBuf[startOverlapLen], SizeOf(SmallInt)*mainAreaLen); Move(endOverlap[0], tmpBuf[startOverlapLen+mainAreaLen], SizeOf(SmallInt)*endOverlapLen); Result := tmpBuf; end;

The move also tries to take the address element 0 for arrays when they are
0. The for loop doesn't execute in that case.

Žilvinas Ledas
to Marco van de Voort
Marco van de Voort wrote
The move also tries to take the address element 0 for arrays when they are 0. The for loop doesn't execute in that case.

Thanks! Didn't thought about that.

Regards,
Žilvinas