SmartPascal
SizeOf
Function
Gives the storage byte size of a type or variable System unit
1  function SizeOf ( Variable : Any type ) : Integer;
2  function SizeOf ( Type ) : Integer;
Description
The SizeOf function returns the storage size, in bytes, of either a Variable or Type.
 
It is often useful to know exactly how much space data is taking. This is especially true when using routines like GetMem.
 
Some types always give the size of a pointer, since they are just that - pointers to where the type data is stored. For example, strings.
 
Use the InstanceSize method to get the size of an object.
Related commands
Length Return the number of elements in an array or string
SetLength Changes the size of a string, or the size(s) of an array
 
Example code : Show the sizes of some data types and variables
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  intNumber : Integer;
  extNumber : Extended;
  sentence  : string;

begin
  // Display the sizes of a number of data types
  ShowMessageFmt('  SizeOf(Integer) = %d',[SizeOf(Integer)]);
  ShowMessageFmt('SizeOf(intNumber) = %d',[SizeOf(intNumber)]);
  ShowMessageFmt(' SizeOf(Extended) = %d',[SizeOf(Extended)]);
  ShowMessageFmt('SizeOf(extNumber) = %d',[SizeOf(extNumber)]);

  // String types and variables are pointers to the actual strings
  sentence := 'A long sentence, certainly longer than 4';
  ShowMessageFmt('   SizeOf(string) = %d',[SizeOf(string)]);
  ShowMessageFmt(' SizeOf(sentence) = %d',[SizeOf(sentence)]);
end;
 
end.
Hide full unit code
     SizeOf(Integer) = 4
   SizeOf(intNumber) = 4
   SizeOf(Extended) = 10
   SizeOf(extNumber) = 10
      SizeOf(string) = 4
   SizeOf(sentence) = 4
 
Example code : Use GetMem to allocate storage for ten records
type
  TRecord = Record
    name : string[10];
    age  : Byte;
  end;

var
  recStorage : PChar;

begin
  // Show the size of our record type
  ShowMessageFmt(' SizeOf(TRecord) = %d',[SizeOf(TRecord)]);

  // Allocate storage for ten of these records
  GetMem(recStorage, 10 * SizeOf(TRecord));
end;
Show full unit code
   SizeOf(TRecord) = 12