[fpc-pascal] Using Move() with dynamic arrays -> RunError(0)

Marco van de Voort marcov at stack.nl
Wed Dec 9 00:00:12 CET 2009


In our previous episode, Žilvinas Ledas said:
> 
> 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.



More information about the fpc-pascal mailing list