目录

Pascal - Memory

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

动态分配内存

在进行编程时,如果您了解数组的大小,那么它很容易,您可以将其定义为数组。 例如,要存储任何人的姓名,它最多可以包含100个字符,因此您可以按如下方式定义内容 -

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

但现在,让我们考虑一种情况,您不知道需要存储的文本的长度,例如,您想要存储有关主题的详细说明。 在这里,我们需要定义一个指向string的指针,而不需要定义需要多少内存。

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提供了使用new.过程释放动态创建的变量的过程new. 如果使用getmem子程序分配了内存,则需要使用子程序freemem释放该内存。 freemem子程序具有以下语法 -

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

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

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

以下是使用ReAllocMemfreemem子程序的示例 -

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

内存管理函数 (Memory Management Functions)

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位模式填充内存区域

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;

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

22

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

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

23

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

在内存范围中查找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;

返回堆栈段寄存器值

↑回到顶部↑
WIKI教程 @2018