[SOLVED] Why Move(); needs pointers dereferenced? lazarus / 62-General ---------------------------------------------------------------------- Blaazen · 2014-08-26 09:28 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: [Select]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; Thanks. ---------------------------------------------------------------------- howardpc · 2014-08-26 09:36 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 · 2014-08-26 09:46 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 · 2014-08-26 09:48 QuoteBecause 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.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 · 2014-08-26 09:54 BTW, this works too: Code: [Select]Move(PByte(pSrc)^, PByte(pHelp)^, 16); QuoteYou 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. 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? ---------------------------------------------------------------------- marcov · 2014-08-26 10:15 Quote from: Blaazen on August 26, 2014, 11:54:28 amThen 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? The parameter construct if move (without type) is called a  "formal parameter".  The compiler passes the address of what is given as parameter to the move procedure, IOW it is a by-reference construct. This means that the address of the expression is passed (the address where the pointer value is stored) and not the value (the contents of the variable is passed on) The base principle is not too different from a VAR parameter (by reference) and without (by value). The main difference is that formal parameters take any type for which an address can be calculated.   Even an untyped pointer only takes any pointer expression by value. ---------------------------------------------------------------------- Blaazen · 2014-08-26 10:19 Thanks. That makes sense. EDIT: I marked it as [SOLVED] but feel free to continue discussion. EDIT2: Now I see, it's the same what Leledumbo tried to tell me. ---------------------------------------------------------------------- BrunoK · 2014-08-26 11:07 D:\fpc-laz\fpc-2.6.4\rtl\inc\systemh.inc Procedure Move(const source;var dest;count:SizeInt); For (const source;var dest) the compiler extracts the addresses of source and dest (it does something like @SomeSource and @SomeDest) then passes them on the stack (or thru registers) to the move procedure. That procedure expects pointers to the effective memory being moved and its target. When specified as pSrc^, pHelp^  ( @pSrc^, @pHelp^ are passed ) it says to the compiler where source and dest are located in the heap (getmem memory space) and that is those addresses that are source and dest in heap allocated memory. Move(pSrc, pDest, 16); would push the address of the pSrc and pDest (@pSrc and @pDest) local variables to the stack for the move procedure and that would cause a copy of 16 bytes of Button1Click(Sender: TObject); >>>LOCAL VAR's (stack) <<< on, more or less themselves, thus destroying the local stack and quite likely crash the application because it would copy to the stack RETURN address or maybe destroy the pointers to pass to FreeMem. I don't know if my comments add anything to the discussion, but what I can tell you is that when I haven't used pointers for a week or 2, I HAVE TO 'LEARN' THEM AGAIN, and that has been going on for the last 16 years with Delphi or FPC. Pointers are regular C programmer stuff, but once the pascal way is well understood it is comparatively clearer. ---------------------------------------------------------------------- Blaazen · 2014-08-26 11:36 Thanks for further explanation. QuoteMove(pSrc, pDest, 16); would push the address of the pSrc and pDest (@pSrc and @pDest) local variables to the stack for the move procedure and that would cause a copy of 16 bytes of Button1Click(Sender: TObject); >>>LOCAL VAR's (stack) <<< on, more or less themselves, thus destroying the local stack and quite likely crash the application because it would copy to the stack RETURN address or maybe destroy the pointers to pass to FreeMem. Exactly what happened here to me. ---------------------------------------------------------------------- Basile B. · 2014-08-28 02:49 In the same fashion, I always asked myself why the heck , still in Move(), the parameter dest is before src  compat with c memmove ? because of the inline asm ? ---------------------------------------------------------------------- Leledumbo · 2014-08-28 04:20 Quote from: Basile B. on August 28, 2014, 04:49:30 amIn the same fashion, I always asked myself why the heck , still in Move(), the parameter dest is before src  compat with c memmove ? because of the inline asm ? Ask the original author. But I believe it exists since TP days, which is written by Anders, and do you what he's specialized in, at least in his early TP days? x86 assembly. And what do you think this convention reflect? That's right, x86 mnemonic ---------------------------------------------------------------------- hinst · 2014-08-28 09:08 Quote from: Basile B. on August 28, 2014, 04:49:30 amIn the same fashion, I always asked myself why the heck , still in Move(), the parameter dest is before src  compat with c memmove ? because of the inline asm ? why? it is not: Code: [Select]Procedure Move(const source;var dest;count:SizeInt);source comes first I believe that in some functions destination comes first to mimic assignment: Code: [Select]Move(a, b); // like a := b ---------------------------------------------------------------------- Blaazen · 2014-08-28 10:56 Yes, hinst is right. In FPC it is Move(src, dest, size); while original C version have memcpy(dest, src, size);