RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.SizeOf Function

Returns the number of bytes occupied by a variable or type.

Pascal
function SizeOf(X): Integer;
C++
int SizeOf( X);

Pass a Delphi variable reference to SizeOf to determine the number of bytes used to represent the variable. Pass a type identifier to SizeOf to determine the number of bytes used to represent instances of that type. SizeOf is useful for determining the amount of memory to specify for the FillChar, Move, or GetMem procedures.  

SizeOf returns 0 when the argument is an untyped variable.  

Delphi Examples: 

 

{
This example fills a char array with characters and then
assigns the array to a text edit.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  S: array[0..79] of AnsiChar;
begin
  { Set to all Xs }
  FillChar(S, SizeOf(S), Ord('X'));
  Edit1.Text := S;
end;
{
This example makes a record.  Reports the size of the reocrd,
fills it and reports the contents.
}
type
  CustRec = record
    Name: string[30];
    Phone: string[14];
  end;

procedure TForm1.Button1Click(Sender: TObject);
 var
  P: ^CustRec;
  size: Integer;
begin
  GetMem(P, SizeOf(CustRec));
  Canvas.TextOut(10, 10, 'The size of the record is ' + IntToStr(SizeOf(CustRec)));
  P.Name := 'a12345678901234567890123456789';
  P.Phone := '9,1,4085551212';
  Canvas.TextOut(10, 40, 'Name was assigned this string: ' + P.Name);
  Canvas.TextOut(10, 70, 'Phone was assigned this string: ' + P.Phone);
  size := SizeOf(CustRec);
  FreeMem (P, size);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!