Forum > General

[SOLVED] Why Move(); needs pointers dereferenced?

(1/3) > >>

Blaazen:
Can someone explain me why Move(const Src; var Dest; Length); needs the pointers dereferenced?
I expected Move(pSrc, pDest, 16); but it doesn't work. See the code. When I type pSrc^, it's the value on that address, isn't it? But it works.


--- Code: ---procedure TForm1.Button1Click(Sender: TObject);
var pSrc, pHelp, pDest: Pointer;
    i: Integer;
begin
  GetMem(pSrc, 16);
  GetMem(pDest, 16);
  for i:=0 to 15 do
    PByte(pSrc+i)^:=i;
  for i:=0 to 15 do
    writeln(PByte(pDest+i)^);
  pHelp:=pDest;
  Move(pSrc^, pHelp^, 16);  //HERE
  for i:=0 to 15 do
    writeln(PByte(pDest+i)^);
  FreeMem(pSrc, 16);
  FreeMem(pDest, 16);
end;
--- End code ---

Thanks.

howardpc:
Because you don't want Move to copy the value of the pointers in your program (some arbitrary location determined by the OS), but to copy the contents of the memory they point to.

Leledumbo:
You need to understand how untyped parameters work. They're passed by reference, but inside the routine, they're still treated as a normal value. Which means, you don't care about its address, but its value. So you set the first value to move/be moved, not the pointer to it.

Blaazen:

--- Quote ---Because you don't want Move to copy the value of the pointers in your program (some arbitrary location determined by the OS), but to copy the contents of the memory they point to.
--- End quote ---
Yes, I agree. But when I type pSrc^ and Dest^, it is the value on that address, not the address itself, so how can the procedure Move(); know where is the source and destination?

Blaazen:
BTW, this works too:

--- Code: ---Move(PByte(pSrc)^, PByte(pHelp)^, 16);
--- End code ---


--- Quote ---You need to understand how untyped parameters work. They're passed by reference, but inside the routine, they're still treated as a normal value. Which means, you don't care about its address, but its value. So you set the first value to move/be moved, not the pointer to it.

--- End quote ---
Then I don't understand well.
I got variable: Pointer. It's somewhere in memory.
Its value is address, it points to another memory, where I got my 16 bytes.
Once I do PByte(pSrc)^ I got value of the very first byte (of that 16B) and the information about the original address is lost, isn't it?

Navigation

[0] Message Index

[#] Next page

Go to full version