DelphiBasics
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
 Download this web site as a Windows program.

Development Boards at very low prices with free postage.


 
Example code : Show the sizes of some data types and variables
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;
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
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
 
 
Delphi Programming Neil Moffatt 2002 - 2014. All rights reserved.  |  Contact the author  |  Home Page