[fpc-pascal] Re: classes initialization

L L at z505.com
Tue Mar 25 14:40:51 CET 2008


I wrote:
> What about old borland objects (heap and stack initialization) process?
>
program test1;

type
  trec = record i: integer; end;

  tclass = class i: integer; end;
 
  tobj = object i: integer; end; 
  pobj = ^Tobj;
 
procedure show;
var rec: trec; c: tclass; o: tobj; po: pobj;  
begin
  writeln('Record local:', rec.i);
  c:= tclass.create;
  new(po);
  writeln('Class local:', c.i); 
  writeln('Stack Object local:', o.i);   
  writeln('Heap Object local:', po^.i);     
  c.free; c:= nil;
  dispose(po);
end;

var rec: trec; c: tclass; o: tobj; po: pobj;
begin
  show;
  writeln('Record global:', rec.i);
  c:= tclass.create; 
  new(po);
  writeln('Class global:', c.i); 
  writeln('Stack Object global:', o.i);     
  writeln('Heap Object global:', po^.i);       
  c.free; c:= nil; 
  dispose(po); 
  readln;
end.


Not a fool proof test since random values have a chance of being zero, 
but it basically shows:
1. global old heap object NOT zeroed
2. local old heap object NOT zeroed
3. local class zeroed
4. global class zeroed
5. global old stack object zeroed
6. local old stack object NOT zeroed
7. global stack record zeroed
8. local stack record NOT zeroed.

I didn't bother with heap records but we can see what will happen

--
L505




More information about the fpc-pascal mailing list