New as function

Frank Heckenbach ih8mj at fjf.gnu.de
Fri Jul 22 16:45:15 CEST 2005


Waldek Hebisch wrote:

> Peter N Lewis wrote:
> > Does New as a function return nil or does it still give a runtime error?
> > 
> > If I want to allocate memory, but handle it gracefully if it fails, 
> > what are the recommended approaches?
> > 
> 
> The modern way is to raise an exception. Unfortunatly, GPC still
> do not support exceptions (I have started working on this, but my
> code ATM is non-working). 
> 
> Lacking exceptions you can just jump out of memory allocator
> (if you provide your own) or error handler (IIRC GPC allows you to
> install custom error handler).

You could also trap runtime errors, using `AtExit', or use the
`Trap' unit (which does this internally). This comes somewhat closer
to exception, though it's not quite the same. Indeed, this may be
the best solution available. It's a bit code to write, but it uses
`New' and has no disadvantages in this regard.

program Trap2Demo;

uses Trap;

type
  p = ^t;
  t = record
    Next: p;
    Data: array [1 .. 50000000] of Integer
  end;

function SafeAllocate = Res: p;

  procedure TryAllocate (Trapped: Boolean);
  begin
    if Trapped then
      begin
        TrappedExitCode := 0;
        TrappedErrorAddr := nil;
        Res := nil
      end
    else
      New (Res)
  end;

begin
  TrapExec (TryAllocate)
end;

var
  i: Integer;
  v, w: p;

begin
  w := nil;
  for i := 1 to 5 do
    begin
      if i > 1 then WriteLn ('Starting again');
      repeat
        v := SafeAllocate;
        if v <> nil then
          begin
            WriteLn ('Allocated v at address ', PtrCard (v));
            v^.Next := w;
            w := v
          end
      until v = nil;
      WriteLn ('Error occurred, continuing');
      while w <> nil do
        begin
          v := w;
          w := w^.Next;
          WriteLn ('Disposing v at address ', PtrCard (v));
          Dispose (v)
        end
    end
end.

Frank

-- 
Frank Heckenbach, frank at g-n-u.de, http://fjf.gnu.de/, 7977168E
GPC To-Do list, latest features, fixed bugs:
http://www.gnu-pascal.de/todo.html
GPC download signing key: ACB3 79B2 7EB2 B7A7 EFDE  D101 CD02 4C9D 0FE0 E5E8




More information about the Gpc mailing list