Recent

Author Topic: [SOLVED] Trying to allocate memory on the heap without exceptions/runtime errors  (Read 468 times)

furious programming

  • Hero Member
  • *****
  • Posts: 675
I don't need it, but I'm curious. Is it possible in Free Pascal to allocate a block of data on the heap and get a pointer to it, but if there is any problem with the allocation, then no exception and runtime error will be emitted? If the block cannot be allocated, the result should be a nil pointer, nothing more. Example of use:

Code: Pascal  [Select][+][-]
  1.   function TryGetMem(ASize: PtrUInt): Pointer;
  2.   begin
  3.     // Some implementation, no support of runtime errors and exceptions.
  4.     // Just return "nil" of the block cannot be allocated.
  5.   end;
  6.  
  7. var
  8.   Block: Pointer;
  9. begin
  10.   // Try to allocate a block of memory on the heap and get pointer to it.
  11.   Block := TryGetMem(4096);
  12.  
  13.   // Test if the memory block was allocated successfully.
  14.   if Block <> nil then
  15.   begin
  16.     // Memory is allocated, so use it.
  17.  
  18.     FreeMem(Block);
  19.   end
  20.   else
  21.     // The block was not allocated, so handle this situation yourself.

Can something like this be done or does a runtime error or exception always have to be emitted?
« Last Edit: December 03, 2022, 03:24:03 pm by furious programming »
Lazarus 2.2.4 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2025.

paweld

  • Hero Member
  • *****
  • Posts: 552
Code: Pascal  [Select][+][-]
  1. function TryGetMem(ASize: PtrUInt): Pointer;
  2. begin
  3.   ReturnNilIfGrowHeapfails := True; //add this
  4.   GetMem(Result, ASize);  
  5. end;
 
https://www.freepascal.org/docs-html/rtl/system/getmem.html and https://www.freepascal.org/docs-html/rtl/system/returnnilifgrowheapfails.html
« Last Edit: December 03, 2022, 02:01:51 pm by paweld »
Best regards / Pozdrawiam
paweld

furious programming

  • Hero Member
  • *****
  • Posts: 675
Ah, it is so easy. Thank you very much your answear.  8)
« Last Edit: December 03, 2022, 03:33:19 pm by furious programming »
Lazarus 2.2.4 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2025.

jamie

  • Hero Member
  • *****
  • Posts: 5068
isn't that what the TRY ... Except .... END is for?
The only true wisdom is knowing you know nothing

furious programming

  • Hero Member
  • *****
  • Posts: 675
isn't that what the TRY ... Except .... END is for?

It doesn't matter — I wanted to know if it is possible to avoid runtime errors and exceptions.
Lazarus 2.2.4 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2025.

Thaddy

  • Hero Member
  • *****
  • Posts: 12618
isn't that what the TRY ... Except .... END is for?

It doesn't matter — I wanted to know if it is possible to avoid runtime errors and exceptions.
Well, you actually can: logic before touching a keyboard. That is no joke.
The only thing I can say about Putin - born st Petersburg- is that he is indeed Russian, as opposed to Stalin, who was Georgian. Depending of historical time frame they could both be Lithuanian or Polish...even German. Shut him up!

jamie

  • Hero Member
  • *****
  • Posts: 5068
I can only conclude that someone does not know how to use the Exception handling.

Thats fine, whatever works for you.
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 4887
    • Bart en Mariska's Webstek
Exceptions are expensive.
If you can avoid that in favour of some error indicator returned as function result or a parameter than that could be benificial.
This why we have several TryXXXToStr functions that do not do something like:
Code: [Select]
  try
    XXXToStr
  except
    Result := False;
  end;
but use the mechanisme I described to return False.

Bart

furious programming

  • Hero Member
  • *****
  • Posts: 675
I can only conclude that someone does not know how to use the Exception handling.

If that's your logic, I have nothing but sympathy for you. I asked a simple question — how to allocate a block of data, and if it is impossible for any reason (e.g. there is not enough available free memory), the nil must be returned, without any runtime errors and exception. This is a simple, specific and detailed question, but you prefer to make strange theories and impute ignorance to me, instead of answering the question (which I didn't expect anyway, because @paweld already showed how to do it and I marked the topic as solved).

I wrote in the first sentence of the first post that I don't need it for anything — I'm just asking out of pure curiosity. Peace.
« Last Edit: December 03, 2022, 08:25:05 pm by furious programming »
Lazarus 2.2.4 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2025.

 

TinyPortal © 2005-2018