Name

SizeOf Function

Syntax

function SizeOf(const Value or type): Integer;

Description

The SizeOf function returns the number of bytes that Value or type occupies, which can be an expression or a type identifier. SizeOf is not a real function.

Tips and Tricks

  • A common use for SizeOf is when calling GetMem, so you know how much memory to request.

  • The size of a pointer, object reference, or class reference is 4 because a pointer fits into 4 bytes (but future versions of Delphi might require more memory to store a pointer).

  • To learn the number of bytes an object takes up, call the class’s InstanceSize method.

Example

var
  P: ^Integer;
begin
  // Allocate memory for 32 integers.
  GetMem(P, 32 * SizeOf(Integer)); /

See Also

GetMem Procedure, Length Function, TObject Type, Type Keyword

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.