操作内存的一些函数

本文详细介绍了几种常用的内存操作函数,包括CopyMemory和MoveMemory的使用方法及注意事项。通过实例展示了如何利用这些函数来实现内存块的复制和移动,并对比了它们在处理重叠内存区域时的不同表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.CopyMemory

CopyMemory()

函数功能描述:将一块内存的数据从一个位置复制到另一个位置

Delphi 函数原型 procedure CopyMemory(Destination: Pointer; Source: Pointer; Length: DWORD);

C++ 函数原型

VOID CopyMemory(

PVOID Destination,

CONST VOID *Source,

DWORD Length

);

参数

Destination

要复制内存块的目的地址。

Source

要复制内存块的源地址。

Length

指定要复制内存块的大小,单位为字节

返回值

该函数为VOID型,没有返回值。

备注

如果目的块与源块有交叠,结果是不可预料的,使用MoveMemory可以解决这个问题。

2.MoveMemory

使用MoveMemory复制数据
procedure MoveMemory(Destination: Pointer; Source: Pointer; Length: DWORD);
MoveMemory参数意义:(1)Destination:目的数据的地址;(2)Source:来源数据的
地址;(3)Length:数据的字节数

var Rect1,Rect2:TRect;
begin
  Rect1.Left:=10;
  Rect1.Top:=10;
  Rect1.Bottom:=200;
  Rect1.Right:=200;

  Rect2.Left:=50;
  Rect2.top:=100;
  Rect2.Bottom:=400;
  Rect2.Right:=390;
  form1.Canvas.FillRect(Rect1);
  showmessage('movemory!');
  form1.Refresh;

  windows.MoveMemory(@rect1,@rect2,sizeof(TRect));
  form1.Canvas.FillRect(rect1);
end;
结果把rect2的值赋到rect1中

移动数组的值
一般的办法:

var s1,s2:array[0..4] of integer;
    i:integer;
begin
  s1[0]:=1;
  s1[1]:=2;
  s1[2]:=3;
  s1[3]:=4;
  s1[4]:=5;

  s2[0]:=6;
  s2[1]:=7;
  s2[2]:=8;
  s2[3]:=9;
  s2[4]:=0;
  for a:=0 to 4 do s1[a]:=s2[a];
end;

用movemeory的办法:

var s1,s2:array[0..4] of integer;
    i:integer;
begin
  s1[0]:=1;
  s1[1]:=2;
  s1[2]:=3;
  s1[3]:=4;
  s1[4]:=5;

  s2[0]:=6;
  s2[1]:=7;
  s2[2]:=8;
  s2[3]:=9;
  s2[4]:=0;
  for i:=0 to 4 do showmessage('s1的'+inttostr(i)+':'+inttostr(s1[i]));
  showmessage('movemeory1!');
  windows.MoveMemory(@s1,@s2,sizeof(s1));

  for i:=0 to 4 do showmessage('s1的'+inttostr(i)+':'+inttostr(s1[i]));
end;

 

procedure FillMemory(Destination: Pointer; Length: DWORD; Fill: Byte);
{$EXTERNALSYM FillMemory}
procedure ZeroMemory(Destination: Pointer; Length: DWORD);
{$EXTERNALSYM ZeroMemory}

procedure       Move( const Source; var Dest; count : Integer );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值