Pascal-memory
パスカル-メモリ管理
この章では、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には、プロシージャ* new。を使用して動的に作成された変数を解放するプロシージャ *dispose が用意されています。 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での低レベルプログラミングの実装に使用されるメモリ管理機能を提供します。 これらの関数の多くは実装に依存しています。 無料のパスカルは、メモリ管理のために以下の機能と手順を提供します-
S.N | Function Name & Description |
---|---|
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; オープン配列または列挙型の最高インデックスを返します |
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; スタックセグメントレジスタ値を返します |