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
因此,您拥有完全的控制权,并且可以在分配内存时传递任何大小值,这与数组不同,一旦定义了大小,就无法更改。
调整大小并释放内存
当你的程序出来时,操作系统会自动释放你的程序分配的所有内存,但作为一个好习惯,当你不再需要内存时,你应该释放该内存。
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提供了以下用于内存管理的函数和程序 −
| S.N | 函数名称和描述 |
|---|---|
| 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 位模式填充内存区域 |
| 15 |
procedure FillWord( var x; count: SizeInt; Value: Word);
用 16 位模式填充内存区域 |
| 16 |
procedure Freemem( p: 指针; 大小: PtrUInt); 释放分配的内存 |
| 17 |
procedure Freemem( p: pointer ); 释放分配的内存 |
| 18 |
procedure Getmem( out p: 指针; 大小: PtrUInt); 分配新内存 |
| 19 |
procedure Getmem(out p:指针); 分配新内存 |
| 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; 在内存范围内查找字符大小的值 |
| 24 |
function IndexDWord( const buf; len: SizeInt; b: DWord):SizeInt; 在内存范围内查找双字大小(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: 指针); 为变量动态分配内存 |
| 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: 指针; 大小: PtrUInt): 指针; 调整堆上内存块的大小 |
| 36 |
function Seg(var X):LongInt; 返回部分 |
| 37 |
procedure SetMemoryManager( const MemMgr: TMemoryManager ); 设置内存管理器 |
| 38 |
function Sptr: Pointer; 返回当前堆栈指针 |
| 39 |
function Sseg:Word; 返回堆栈段寄存器值 |

