tobjsize.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {$static on}
  2. type
  3. pbaseclass = ^tbaseclass;
  4. pderivedclass = ^tderivedclass;
  5. tbaseclass = object
  6. x : longint;
  7. constructor init;
  8. function getsize : longint; static;
  9. function getsize2 : longint;
  10. procedure check_size; virtual;
  11. procedure static_check_size; static;
  12. procedure check_normal;
  13. procedure check_static; static;
  14. procedure check_virtual; virtual;
  15. destructor done; virtual;
  16. end;
  17. tderivedclass = object(tbaseclass)
  18. y : longint;
  19. procedure check_size; virtual;
  20. end;
  21. const
  22. has_error : boolean = false;
  23. expected_size_for_tbaseclass = sizeof(pointer) + sizeof(longint);
  24. expected_size_for_tderivedclass = sizeof(pointer) + 2*sizeof(longint);
  25. var
  26. basesize : longint;
  27. derivedsize : longint;
  28. constructor tbaseclass.init;
  29. begin
  30. end;
  31. destructor tbaseclass.done;
  32. begin
  33. end;
  34. function tbaseclass.getsize : longint;
  35. begin
  36. getsize:=sizeof(self);
  37. end;
  38. function tbaseclass.getsize2 : longint;
  39. begin
  40. getsize2:=sizeof(self);
  41. end;
  42. procedure tbaseclass.check_size;
  43. begin
  44. if sizeof(self)<>getsize then
  45. begin
  46. Writeln('Compiler creates garbage');
  47. has_error:=true;
  48. end;
  49. if sizeof(self)<>getsize2 then
  50. begin
  51. Writeln('Compiler creates garbage');
  52. has_error:=true;
  53. end;
  54. end;
  55. procedure tbaseclass.static_check_size;
  56. begin
  57. if sizeof(self)<>getsize then
  58. begin
  59. Writeln('Compiler creates garbage');
  60. has_error:=true;
  61. end;
  62. end;
  63. procedure tbaseclass.check_normal;
  64. begin
  65. check_size;
  66. static_check_size;
  67. end;
  68. procedure tbaseclass.check_static;
  69. begin
  70. {check_size;}
  71. static_check_size;
  72. end;
  73. procedure tbaseclass.check_virtual;
  74. begin
  75. check_size;
  76. static_check_size;
  77. end;
  78. procedure tderivedclass.check_size;
  79. begin
  80. Writeln('Calling tderived check_size method');
  81. inherited check_size;
  82. end;
  83. var
  84. cb : tbaseclass;
  85. cd : tderivedclass;
  86. c1 : pbaseclass;
  87. begin
  88. cb.init;
  89. cd.init;
  90. new(c1,init);
  91. basesize:=sizeof(cb);
  92. Writeln('Sizeof(cb)=',basesize);
  93. if basesize<>expected_size_for_tbaseclass then
  94. Writeln('not the expected size : ',expected_size_for_tbaseclass);
  95. derivedsize:=sizeof(cd);
  96. Writeln('Sizeof(ct)=',derivedsize);
  97. if derivedsize<>expected_size_for_tderivedclass then
  98. Writeln('not the expected size : ',expected_size_for_tderivedclass);
  99. cb.check_size;
  100. cd.check_size;
  101. c1^.check_size;
  102. cb.static_check_size;
  103. cd.static_check_size;
  104. c1^.static_check_size;
  105. tbaseclass.static_check_size;
  106. tderivedclass.static_check_size;
  107. tbaseclass.check_static;
  108. tderivedclass.check_static;
  109. cb.check_normal;
  110. cb.check_static;
  111. cb.check_virtual;
  112. cd.check_normal;
  113. cd.check_static;
  114. cd.check_virtual;
  115. dispose (c1,done);
  116. c1:=new(pderivedclass,init);
  117. c1^.check_size;
  118. c1^.static_check_size;
  119. dispose (c1,done);
  120. if has_error then
  121. begin
  122. Writeln('Error with class methods');
  123. halt(1);
  124. end;
  125. end.