Pascal - 内存管理

本章介绍Pascal中的动态内存管理. Pascal编程语言为内存分配和管理提供了几种功能.

动态分配内存

在进行编程时,如果你知道一个大小的数组,然后很容易,你可以将它定义为一个数组.例如,要存储任何人的姓名,它最多可以包含100个字符,因此您可以定义以下内容和减号;

 
 var 
 name:array [1..100] of char;

但是现在,让我们考虑一种情况,你不知道你需要存储的文本的长度,例如,你想要存储关于主题的详细描述.在这里,我们需要定义一个指向字符串的指针,而不需要定义需要多少内存.

Pascal提供了一个过程 new 来创建指针变量.

program exMemory;
var
name: array[1..100] of char;
description: ^string;

begin
   name:= 'Zara Ali';
   
   new(description);
      if not assigned(description) then
         writeln(' Error - unable to allocate required memory')
      else
         description^ := 'Zara ali a DPS student in class 10th';
   writeln('Name = ', name );
   writeln('Description: ', description^ );
end.

编译并执行上述代码时,会产生以下结果 :

Name = Zara Ali
Description: Zara ali a DPS student in class 10th

现在,如果你需要定义一个指针要使用稍后要引用的特定字节数,您应该使用 getmem 函数或 getmem 过程,该过程具有以下语法 :

procedure Getmem(
   out p: pointer;
   Size: PtrUInt
);

function GetMem(
   size: PtrUInt
):pointer;

在前面的例子中,我们声明了一个指向字符串的指针.字符串的最大值为255个字节.如果你真的不需要那么多的空间或更大的空间,就字节而言, getmem 子程序允许指定它.让我们重写上一个例子,使用 getmem :

program exMemory;
var
name: array[1..100] of char;
description: ^string;

begin
   name:= 'Zara Ali';
   
   description := getmem(200);
      if not assigned(description) then
         writeln(' Error - unable to allocate required memory')
      else
         description^ := 'Zara ali a DPS student in class 10th';
   writeln('Name = ', name );
   writeln('Description: ', description^ );
   
   freemem(description);
end.

编译并执行上述代码时,会产生以下结果 :

Name = Zara Ali
Description: Zara ali a DPS student in class 10th

所以,你有完全的控制权,你可以传递任何大小的值,同时分配内存不同于数组,一旦你定义的大小不能更改.

调整大小和释放内存

当你的程序到来时out,操作系统自动释放你的程序分配的所有内存,但是当你不再需要内存时这是一个好习惯,那么你应该释放那个内存.

Pascal提供了过程 dispose 使用 new过程释放动态创建的变量.如果使用 getmem 子程序分配了内存,则需要使用子程序 freemem 释放此内存. freemem 子程序具有以下语法 :

procedure Freemem(
   p: pointer;
  Size: PtrUInt
);

function Freemem(
   p: pointer
):PtrUInt;

或者,您可以通过调用函数 ReAllocMem 来增加或减少已分配内存块的大小.让我们再次检查上面的程序,并使用 ReAllocMem freemem 子程序.以下是 ReAllocMem :

function ReAllocMem(
   var p: pointer;
   Size: PtrUInt
):pointer;

以下是使用 ReAllocMem freemem 子程序 的示例:

program exMemory;
var
name: array[1..100] of char;
description: ^string;
desp: string;

begin
   name:= 'Zara Ali';
   desp := 'Zara ali a DPS student.';
   
   description := getmem(30);
      if not assigned(description) then
         writeln('Error - unable to allocate required memory')
      else
         description^ := desp;

   (* Suppose you want to store bigger description *)
   description := reallocmem(description, 100);
   desp := desp + ' She is in class 10th.';
   description^:= desp; 
   
   writeln('Name = ', name );
   writeln('Description: ', description^ );
   
   freemem(description);
end.

编译并执行上述代码时,会产生以下结果 :

Name = Zara Ali
Description: Zara ali a DPS student. She is in class 10th

内存管理函数

Pascal提供了大量用于实现的内存管理功能各种数据结构并在Pascal中实现低级编程.其中许多功能都依赖于实现. Free Pascal为内存管理提供以下函数和过程 :

SN功能名称&描述
1

function Addr(X: TAnytype):Pointer;

返回变量的地址

2

function Assigned(P: Pointer):Boolean;

检查指针是否有效

3

function CompareByte(const buf1; const buf2; len: SizeInt):SizeInt;

比较每个字节2个内存缓冲区字节

4

function CompareChar(const buf1; const buf2; len: SizeInt):SizeInt;

比较每个字节2个内存缓冲区字节

5

function CompareDWord(const buf1; const buf2; len: SizeInt):SizeInt;

比较每个字节2个内存缓冲区字节

6

function CompareWord(const buf1; const buf2; len: SizeInt):SizeInt;

比较每个字节2个内存缓冲区字节

7

function Cseg: Word;

返回代码段

8

procedure Dispose(P: Pointer);

动态分配记忆

9

procedure Dispose(P: TypedPointer; Des: TProcedure);

释放动态分配的内存

10

function Dseg: Word;

返回数据段

11

procedure FillByte(var x; count: SizeInt;  value: Byte);

用8位模式填充内存区域

12

procedure FillChar(  var x;  count: SizeInt; Value: Byte|Boolean|Char);

填充具有特定字符的内存区域

13

procedure FillDWord( var x; count: SizeInt; value: DWord);

用32位模式填充内存区域

14

procedure FillQWord( var x;  count: SizeInt;  value: QWord);

用64位模式填充内存区域

15procedure FillWord(  var x;  count: SizeInt;  Value: Word);

用16位模式填充内存区域

16

procedure Freemem(  p: pointer;  Size: PtrUInt);

释放已分配的内存

17

procedure Freemem(  p: pointer );

发布分配d记忆

18

procedure Getmem( out p: pointer; Size: PtrUInt);

分配新内存

19

procedure Getmem( out p: pointer);

分配新内存

20

procedure GetMemoryManager(  var MemMgr: TMemoryManager);

返回当前内存管理器

21

function High(  Arg: TypeOrVariable):TOrdinal;

返回打开数组或枚举的最高索引

22

function IndexByte(  const buf;  len: SizeInt;  b: Byte):SizeInt;

在内存范围内查找字节大小的值

23

function IndexChar(  const buf;  len: SizeInt;  b: Char):SizeInt;

在a中查找char大小的值记忆范围

24

function IndexDWord(  const buf;  len: SizeInt;  b: DWord):SizeInt;

在内存范围内查找DWord大小(32位)值

25

function IndexQWord(  const buf;  len: SizeInt;  b: QWord):SizeInt;

在内存范围内查找QWord大小的值

26

function Indexword(  const buf;  len: SizeInt;  b: Word):SizeInt;

在内存范围内查找字大小的值

27

function IsMemoryManagerSet:Boolean;

是否设置了内存管理器

28

function Low(  Arg: TypeOrVariable ):TOrdinal;

返回打开数组或枚举的最低索引

29

procedure Move(  const source;  var dest;  count: SizeInt );

将数据从内存中的一个位置移动到另一个位置

30

procedure MoveChar0(  const buf1;  var buf2;  len: SizeInt);

将数据移至第一个零字符

31

procedure New(  var P: Pointer);

为变量动态分配内存

32

procedure New(  var P: Pointer;  Cons: TProcedure);

为变量动态分配内存

33

function Ofs(  var X ):LongInt;

返回变量的偏移量

34

function ptr(  sel: LongInt;  off: LongInt):farpointer;

将段和偏移量组合到指针

35

function ReAllocMem(  var p: pointer;  Size: PtrUInt):pointer;

调整堆上内存块的大小

36

function Seg(  var X):LongInt;

返回分部

37

procedure SetMemoryManager(  const MemMgr: TMemoryManager );

设置内存管理器

38

function Sptr: Pointer;

返回当前堆栈指针

39

function Sseg: Word;

返回堆栈段寄存器值