Select byte array and free it in delphi

I would like to allocate space (dynamic size) with a byte array and get a pointer to "spacearea" and free it later if I no longer need it.

I know about VirtualAlloc, VirutalAllocEx and LocalAlloc. Which one is the best and how can I free memory?

Thank you for your help.

+5
source share
4 answers

I do not think it is a good idea to use winapi for this instead of the built-in Pascal functions.

You can simply define an array of bytes as

var yourarray: array of byte;

then it can be highlighted

setlength(yourarray, yoursize);

and freed

setlength(yourarray, 0);

Such an array is calculated by reference, and you can access individual bytes as yourarray[byteid]

, :

var p: pointer;

GetMem(p, yoursize);

FreeMem(p);
+11

GetMem/FreeMem RawByteString. , GetMem/FreeMem, RawByteString , .

VirtualAlloc/VirtualFree GetMem/FreeMem. ( ) API VirtualAlloc/VirtualFree, , .

VirtualAlloc/VirtualFree , , , , . API VirtualAllocEx/VirtualFreeEx ( , ).

/, GlobalAlloc/GlobalFree API- .

+5

VirtualAlloc - . . , , VirtualAlloc, .

Windows 32 4096 . .

, VirtualAlloc , . VirtualAlloc " ". , , , , VirtualAlloc, .

VirtualAlloc VirtualAllocEx, . , , , , , / , .

VirtualFree , VirtualAlloc.

VirtualAlloc LocalAlloc , LocalAlloc , - . VirtualAlloc, , , , malloc, getmem LocalAlloc.

LocalAlloc , Windows malloc getmem. LocalAlloc malloc ++ getmem Delphi. GetMem Delphi LocalAlloc, DElphi, , .

LocalFree, , LocalAlloc. , .

, . , LocalAlloc getmem, - .

, getmem LocalAlloc, .

Delphi 5 ++ Delphi 5 getmem , . , , , . , , .

, LocalAlloc, malloc getmem, , VirtualAlloc, , LocalAlloc getmem.

Pascal getmem SetLength, . - LocalAlloc - .

+2

, , - WinAPI, . , API , .

If you want to use the Delphi memory manager, GetMemory and FreeMemory are an obvious choice, however if you need your pointer aligned to the size of the system page (which is a requirement for some low-level libraries), or you are going to use large buffer sizes, and then the virtual functions of the Windows API VirtualAlloc and VirtualFree are your best friends.

+1
source

All Articles