How to allocate 1MB in Pascal

1 Answer

0 votes
program AllocateMemory;

var
  MemoryBlock: Pointer; // Pointer to hold the allocated memory
  Size: LongInt;

begin
  Size := 1024 * 1024; // 1 megabyte (1,048,576 bytes)
  GetMem(MemoryBlock, Size); // Allocate memory

  if MemoryBlock <> nil then
  begin
    WriteLn('Memory successfully allocated.');
    // Use the memory here...

    FreeMem(MemoryBlock, Size); // Free the allocated memory
    WriteLn('Memory successfully freed.');
  end
  else
    WriteLn('Memory allocation failed.');
end.


  
(*
run:
  
Memory successfully allocated.
Memory successfully freed.
  
*)  

 



answered May 19, 2025 by avibootz

Related questions

3 answers 258 views
2 answers 218 views
3 answers 224 views
1 answer 160 views
160 views asked May 20, 2025 by avibootz
3 answers 242 views
...