[fpc-pascal] AnsiString setlength and getmem.

Luis Fernando Del Aguila Mejía luis3000 at ec-red.com
Fri Feb 11 23:00:07 CET 2011


(* Program One *)

{$codepage utf8}
{$Mode objfpc}
Uses math,sysutils;
Var cad:ansistring;
    c,n:longword;
Begin
   c:=2**30;  // 1 GiB
   n:=4;          // with  3 & 4 at fpc 2.4.2, no show Out of Memory
                     // with   3 & 4 at fpc 2.5.1, no show Out of Memory 
(02/11/2011 04:04p.m. snapshot)
   try
     setlength(cad,(c*n)-1);
     Writeln('ok')
   Except
    On EOutOfMemory do Writeln('Out of Memory, free more memory');
   End;
End.

(* Program Two *)

{$codepage utf8}
{$Mode objfpc}
Uses math,sysutils;
Var p:^byte;
    c,n:longword; //2**32
Begin
    c:=2**30;  // 1 GiB
    n:=4;        //  with 4 fail at fpc 2.4.2, no Show Out of Memory, but
                     //  at fpc 2.5.1, no  Show Out of Memory, show 
EInvalidPointer (02/11/2011 04:04p.m. snapshot)
 try
  getmem(p,(c*n)-1); //Si n es 5 entonces se trunca y lo crea 
longword((c*n)-1)
  Writeln('ok')
 Except
    On EOutOfMemory do Writeln('Out of Memory, free more memory');
    On EInvalidPointer do  Writeln('Invalid Pointer');
 End;
End.

I'm not sure if they are mistakes.
I know very well the work exceptions.
Program One: Why not show Out of Memory?
Program Two: What should show Out of Memory or Invalid Pointer?

Thanks.




More information about the fpc-pascal mailing list