Gives the storage byte size of a type or variable System unit
1  function SizeOf ( Variable : Any type ) : Integer;
2  function SizeOf ( Type ) : Integer;
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
 Author links

 Download this web site as a Windows program.

Example code : Show the sizes of some data types and variables
  intNumber : Integer;
  extNumber : Extended;
  sentence  : string;

  // 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)]);
Show 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
  TRecord = Record
    name : string[10];
    age  : Byte;

  recStorage : PChar;

  // 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));
Show full unit code
   SizeOf(TRecord) = 12
'); document.write('
'); document.write('
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page