📜  Pascal-内存管理

📅  最后修改于: 2020-11-03 16:20:51             🧑  作者: Mango


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

动态分配内存

在进行编程时,如果您知道数组的大小,那么这很容易,您可以将其定义为数组。例如,要存储任何人的名字,最多可以输入100个字符,因此您可以定义以下内容-

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

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

Pascal提供了一个的过程来创建指针变量。

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

因此,您拥有完全的控制权,并且可以在分配内存的同时传递任何大小值,而与数组不同,在数组中,一旦定义大小就无法更改。

调整大小和释放内存

当您的程序发布时,操作系统会自动释放程序分配的所有内存,但是作为一种好习惯,当您不再需要内存时,则应该释放该内存。

帕斯卡提供程序处置使用过程释放动态创建的变量如果使用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

内存管理功能

Pascal提供了大量的内存管理功能,这些功能用于实现各种数据结构以及在Pascal中实现低级编程。其中许多功能都依赖于实现。 Free Pascal提供以下用于内存管理的功能和过程-

S.N Function Name & Description
1

function Addr(X: TAnytype):Pointer;

Returns address of variable

2

function Assigned(P: Pointer):Boolean;

Checks if a pointer is valid

3

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

Compares 2 memory buffers byte per byte

4

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

Compares 2 memory buffers byte per byte

5

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

Compares 2 memory buffers byte per byte

6

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

Compares 2 memory buffers byte per byte

7

function Cseg: Word;

Returns code segment

8

procedure Dispose(P: Pointer);

Frees dynamically allocated memory

9

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

Frees dynamically allocated memory

10

function Dseg: Word;

Returns data segment

11

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

Fills memory region with 8-bit pattern

12

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

Fills memory region with certain character

13

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

Fills memory region with 32-bit pattern

14

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

Fills memory region with 64-bit pattern

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

Fills memory region with 16-bit pattern

16

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

Releases allocated memory

17

procedure Freemem( p: pointer );

Releases allocated memory

18

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

Allocates new memory

19

procedure Getmem( out p: pointer);

Allocates new memory

20

procedure GetMemoryManager( var MemMgr: TMemoryManager);

Returns current memory manager

21

function High( Arg: TypeOrVariable):TOrdinal;

Returns highest index of open array or enumerated

22

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

Finds byte-sized value in a memory range

23

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

Finds char-sized value in a memory range

24

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

Finds DWord-sized (32-bit) value in a memory range

25

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

Finds QWord-sized value in a memory range

26

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

Finds word-sized value in a memory range

27

function IsMemoryManagerSet: Boolean;

Is the memory manager set

28

function Low( Arg: TypeOrVariable ):TOrdinal;

Returns lowest index of open array or enumerated

29

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

Moves data from one location in memory to another

30

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

Moves data till first zero character

31

procedure New( var P: Pointer);

Dynamically allocate memory for variable

32

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

Dynamically allocates memory for variable

33

function Ofs( var X ):LongInt;

Returns offset of variable

34

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

Combines segment and offset to pointer

35

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

Resizes a memory block on the heap

36

function Seg( var X):LongInt;

Returns segment

37

procedure SetMemoryManager( const MemMgr: TMemoryManager );

Sets a memory manager

38

function Sptr: Pointer;

Returns current stack pointer

39

function Sseg: Word;

Returns stack segment register value