Recent

Author Topic: SizeOf() of an object before the call of the constructor - should this crash?  (Read 1128 times)

Avinash

  • Full Member
  • ***
  • Posts: 119
This code crashes in FPC (3.2.2 Win32), but Turbo Pascal outputs 0:

Code: Pascal  [Select][+][-]
  1. type
  2.   TObject = object
  3.     constructor Init;
  4.   end;
  5.  
  6.   constructor TObject.Init;
  7.   begin
  8.   end;
  9.  
  10. var
  11.   T: TObject;
  12. begin
  13.   {T.Init;}
  14.   WriteLn(SizeOf(T));
  15. end.

ASerge

  • Hero Member
  • *****
  • Posts: 2262
This code crashes in FPC (3.2.2 Win32), but Turbo Pascal outputs 0:
FPC and Delphi (or TP) have a different approach to determining the presence of VMT.
FPC - the object has a constructor or virtual functions.
Delphi - the object has virtual functions (but without a constructor there will be a runtime error).
If you add a virtual function in this example, the exe in Delphi will also fail.
If no virtual functions are expected, then write procedure instead of constructor, then the error will not occur.

Thaddy

  • Hero Member
  • *****
  • Posts: 14625
  • Sensorship about opinions does not belong here.
Adding a virtual destructor done will do the same, btw. Basically anything virtual crashes in both Delphi and FPC. It does not crash in both when initialized through a pointer using New/Dispose of course. This is just for completeness, ASerge explained it very well.
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

 

TinyPortal © 2005-2018