- Pascal 教程
- Pascal - 首页
- Pascal - 概述
- Pascal - 环境设置
- Pascal - 程序结构
- Pascal - 基本语法
- Pascal - 数据类型
- Pascal - 变量类型
- Pascal - 常量
- Pascal - 运算符
- Pascal - 决策
- Pascal - 循环
- Pascal - 函数
- Pascal - 过程
- Pascal - 变量作用域
- Pascal - 字符串
- Pascal - 布尔值
- Pascal - 数组
- Pascal - 指针
- Pascal - 记录
- Pascal - 变体
- Pascal - 集合
- Pascal - 文件处理
- Pascal - 内存
- Pascal - 单元
- Pascal - 日期和时间
- Pascal - 对象
- Pascal - 类
- Pascal 有用资源
- Pascal - 快速指南
- Pascal - 有用资源
- Pascal - 讨论
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 提供了以下用于内存管理的函数和过程:
| 序号 | 函数名称和描述 |
|---|---|
| 1 |
function Addr(X: TAnytype):Pointer; 返回变量的地址 |
| 2 |
function Assigned(P: Pointer):Boolean; 检查指针是否有效 |
| 3 |
function CompareByte(const buf1; const buf2; len: SizeInt):SizeInt; 逐字节比较两个内存缓冲区 |
| 4 |
function CompareChar(const buf1; const buf2; len: SizeInt):SizeInt; 逐字节比较两个内存缓冲区 |
| 5 |
function CompareDWord(const buf1; const buf2; len: SizeInt):SizeInt; 逐字节比较两个内存缓冲区 |
| 6 |
function CompareWord(const buf1; const buf2; len: SizeInt):SizeInt; 逐字节比较两个内存缓冲区 |
| 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: pointer; Size: PtrUInt); 释放已分配的内存 |
| 17 |
procedure Freemem( p: pointer ); 释放已分配的内存 |
| 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; 在内存范围内查找字符大小的值 |
| 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; 返回堆栈段寄存器值 |