[Lazarus] Use of Move()?

Marc Weustink marc at dommelstein.net
Tue Feb 1 23:33:58 CET 2011


On 1-2-2011 23:11, Bo Berglund wrote:
> On Tue, 1 Feb 2011 21:18:57 +0100, Burkhard Carstens<fpc at bcsoft.de>
> wrote:
>
>> Am Dienstag, 1. Februar 2011 21:03 schrieb Bo Berglund:
>>> I think I have made an error.....
>>> I have created a generalized buffer handler for my project and here I
>>> have this function to read data from the buffer.
>>>
>>> function TSSCommBuf.Read(Dest: Pointer; Count: Cardinal): Cardinal;
>>> var
>>>    num: Cardinal;
>>> begin
>>>    num := FWriteIndex - FReadIndex;  //Remaining data bytes in buffer
>>>    if num>= Count then
>>>      num := Count;
>>>    if num>  0 then
>>>    begin
>>>      Move(FBuf[FReadIndex], Dest, num);
>>
>> didn't read further, but: try Dest^ ..
>> (quick guess)
>>
>
> Did not work, but I am using Move() in a way that has worked for me
> using Delphi for many years, so it may have to do with Delphi vs fpc
> differences....

Also in Delphi you must use Dest^, otherwise you are overwriting your 
pointer. (unless they have some overloaded Move)

> What I usually do is the following:
>
> Move(Source, Dest, Count);
> where normally Source is an array, for example a string and Dest is
> for example a multi-byte variable:
>
> Move(SrcArr[Index], DestVar, SizeOf(DestVar);

Just tested:

program MoveTest;
{$mode objfpc}{$H+}
uses
   SysUtils;
var
   S: String;
   I: Integer;
   P: Pointer;
begin
   S := 'Bla';
   S := S + S;
   P := @I;
   Move(S[1], P^, SizeOf(I));

   WriteLn(Format('%x %x %x %x %8.8x', [Ord(S[1]), Ord(S[2]), Ord(S[3]), 
Ord(S[4]), i]));
end.

$ ./movetest
42 6C 61 42 42616C42


Marc




More information about the Lazarus mailing list