Recent

Author Topic: [SOLVED] How to query the size of a Base-Class and a derived Class?  (Read 343 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1047
Let's assume I have a Base-Class 'TClass1' and a derived Class 'TClass2' (and maybe a couple of more derived Classes). I want to be able to query the size of those classes with 2 methods, which I want to declare in the Base-Class:
 - 'showSize1' should always report the size of the Base-Class (even if called from 'TClass2' or later derived Classes)
 - 'showSize2' should always report the size of the current Class.

With 'size' I mean the number of bytes which are allocated on the heap during 'Create' and are freed during 'Free'.

Method 'showSize2' does want I want: is shows the size of the current class.

But Method 'showSize1' does not want I want: is shows the size of a pointer (which I understand, because a Class type is a pointer). What I do not understand: why can't I use something like sizeof(TClass1^)? What must I do instead?

Thanks in advance.

Code: Pascal  [Select][+][-]
  1. type
  2.    TClass1 = Class
  3.       procedure showSize1;
  4.       procedure showSize2;
  5.          private
  6.       v1: integer;
  7.       v2: longint;
  8.    end;
  9.  
  10.    TClass2 = Class(TClass1)
  11.          private
  12.       v3: double;
  13.       v4: string[40];
  14.    end;
  15.  
  16. procedure TClass1.showSize1;
  17.    begin
  18.    writeln(sizeof(self));     // shows the size of a pointer
  19.    writeln(sizeof(TClass1));  // shows the size of a pointer
  20. // writeln(sizeof(self^));    // does not compile
  21. // writeln(sizeof(TClass1^)); // does not compile
  22.    end;
  23.  
  24. procedure TClass1.showSize2;
  25.    begin
  26.    writeln(self.InstanceSize); // shows the size of the current class
  27.    end;
  28.  
  29. procedure test;
  30.    var C1: TClass1;
  31.        C2: TClass2;
  32.    begin
  33.    C1:=TClass1.Create;
  34.    C1.showSize1;
  35.    C1.showSize2;
  36.    C1.Free;
  37.  
  38.    C2:=TClass2.Create;
  39.    C2.showSize1;
  40.    C2.showSize2;
  41.    C2.Free;
  42.    end;
« Last Edit: November 22, 2025, 09:59:41 am by Hartmut »

n7800

  • Hero Member
  • *****
  • Posts: 594
  • Lazarus IDE contributor
    • GitLab profile
Re: How to query the size of a Base-Class and a derived Class?
« Reply #1 on: November 21, 2025, 07:37:07 pm »
"InstanceSize" is a class method (not just a regular method) and can be called from the class type itself (not an instance). Therefore, you can simply call it anywhere in your code:

Code: Pascal  [Select][+][-]
  1. writeln(TClass1.InstanceSize);
  2. writeln(TClass2.InstanceSize);

As far as I know, classes of the same type always allocate the same size of memory, so this doesn't depend on the specific instance.

Moreover, it's unclear which class in the inheritance chain should be considered the "base" class. Theoretically, the base class for all classes is TObject ))
« Last Edit: November 21, 2025, 07:38:55 pm by n7800 »

n7800

  • Hero Member
  • *****
  • Posts: 594
  • Lazarus IDE contributor
    • GitLab profile
Re: How to query the size of a Base-Class and a derived Class?
« Reply #2 on: November 21, 2025, 08:51:39 pm »
To avoid specifying a specific class in each call, you can still do this through a class method. But to make it work as if it were a declared class, rather than an instance class, you can use the static modifier:

Code: Pascal  [Select][+][-]
  1. program project1;
  2. type
  3.   TClass1 = class
  4.     a: integer;
  5.     class function BaseSize: integer; static; // <--- static!
  6.   end;
  7.   TClass2 = class(TClass1)
  8.     b: integer;
  9.   end;
  10.   class function TClass1.BaseSize: integer; static;
  11.   begin
  12.     result := InstanceSize;
  13.   end;
  14. begin
  15.   // "class" size
  16.   WriteLn(TObject.InstanceSize); // 4
  17.   WriteLn(TClass1.InstanceSize); // 8
  18.   WriteLn(TClass2.InstanceSize); // 12
  19.   // instance size
  20.   WriteLn(TObject.Create.InstanceSize); // 4
  21.   WriteLn(TClass1.Create.InstanceSize); // 8
  22.   WriteLn(TClass2.Create.InstanceSize); // 12
  23.   // "base" class size
  24.   WriteLn(TClass1.Create.BaseSize); // 8
  25.   WriteLn(TClass2.Create.BaseSize); // 8 // <--- as in the "base" class
  26. end.
This code is just an example. It contains memory leaks!

I think that's what you meant.

Hartmut

  • Hero Member
  • *****
  • Posts: 1047
Re: How to query the size of a Base-Class and a derived Class?
« Reply #3 on: November 22, 2025, 09:59:24 am »
Thank you very much n7800 for your answer. Both
Code: Pascal  [Select][+][-]
  1. writeln(TClass1.InstanceSize);
and
Code: Pascal  [Select][+][-]
  1. class function BaseSize: integer; static;
do exacty what I searched for.

 

TinyPortal © 2005-2018