Mega Code Archive

 
Categories / Delphi / Examples
 

How can i avoid an access violation when using the move procedure

Question: How can I avoid an access violation when using the move procedure with pointers? Answer: You must dereference the pointers, otherwise you are moving the address of the pointers and possibly corrupting other memory locations. Example procedure TForm1.Button1Click(Sender: TObject); var p1 : pointer; p2 : pointer; begin GetMem(p1, 128); GetMem(p2, 128); {This line may cause an access violation} Move(p1, p2, 128); {This line is correct} Move(p1^, p2^, 128); FreeMem(p1, 128); FreeMem(p2, 128); end;