Главная страница | назад





Article #16127: How can I avoid an access violation when using the move procedure

 Question and Answer Database

FAQ1127D.txt   How can I avoid an access violation when using the move procedure
Category   :Object Pascal
Platform    :All
Product    :All 32 bit  

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;



7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99