ZetCode

Pascal records

last modified November 18, 2026 Free Pascal 3.2.2

A record groups several values of different types under one name. Records are the Pascal structure type: they hold the fields of a point, a person, a date, or a configuration, and they can be nested, passed to procedures, and stored in arrays. This article describes the record declaration, field access, nested records, the with statement, variant records, packed layout, and the managed fields that the compiler frees automatically.

Declaring a record

A record type is declared in a type section. The fields are listed between record and end, each with a name and a type, and the fields are accessed with a dot. The whole record can be assigned to another variable of the same type, which copies all fields.

point.pas
program point;
{$mode objfpc}{$H+}

type
  TPoint = record
    X: Integer;
    Y: Integer;
  end;

var
  p, q: TPoint;

begin
  p.X := 3;
  p.Y := 4;
  q := p;
  q.X := 10;
  WriteLn('p = (', p.X, ', ', p.Y, ')');
  WriteLn('q = (', q.X, ', ', q.Y, ')');
  WriteLn('SizeOf(TPoint) = ', SizeOf(TPoint));
end.

The assignment q := p copies both fields, so changing q.X afterwards does not change p.X. The record occupies eight bytes: two four-byte integers with no padding between them. The T prefix on the type name is a Pascal convention, not a language rule.

$ fpc -gl point.pas
$ ./point
p = (3, 4)
q = (10, 4)
SizeOf(TPoint) = 8

Nested records

A field can itself be a record. The outer field is accessed with one dot and the inner field with another, as in person.address.city. Nested records are the usual way to model a structure that has a structure inside it, such as a person with an address.

nested.pas
program nested;
{$mode objfpc}{$H+}

type
  TAddress = record
    street: string;
    city: string;
    zip: string;
  end;
  TPerson = record
    name: string;
    age: Integer;
    address: TAddress;
  end;

var
  person: TPerson;

begin
  person.name := 'Alice';
  person.age := 30;
  person.address.street := 'Main Street 12';
  person.address.city := 'Bratislava';
  person.address.zip := '811 01';
  WriteLn(person.name, ', ', person.age);
  WriteLn(person.address.street);
  WriteLn(person.address.zip, ' ', person.address.city);
end.

TAddress must be declared before TPerson, because the compiler needs its definition to lay out the outer record. The string fields are managed: the record stores a reference, and the compiler releases the strings when the record goes out of scope. The record itself needs no constructor or destructor.

$ fpc -gl nested.pas
$ ./nested
Alice, 30
Main Street 12
811 01 Bratislava

Records as parameters and results

A record can be passed to a function and returned from one. A const parameter avoids copying a large record and prevents the function from changing it. A function result is assigned field by field, just like a local variable.

param.pas
program param;
{$mode objfpc}{$H+}

type
  TRect = record
    Left, Top, Right, Bottom: Integer;
  end;

function Area(const r: TRect): Integer;
begin
  Result := (r.Right - r.Left) * (r.Bottom - r.Top);
end;

function MakeRect(l, t, r, b: Integer): TRect;
begin
  Result.Left := l;
  Result.Top := t;
  Result.Right := r;
  Result.Bottom := b;
end;

var
  rect: TRect;

begin
  rect := MakeRect(2, 3, 12, 8);
  WriteLn('rect = (', rect.Left, ',', rect.Top, ')-(',
          rect.Right, ',', rect.Bottom, ')');
  WriteLn('area = ', Area(rect));
end.

MakeRect fills the four fields of Result and returns the record by value. Area receives it through a const parameter and computes ten times five, which is 50. Passing a record by value copies it; for a large record with many fields, const or var is preferable.

$ fpc -gl param.pas
$ ./param
rect = (2,3)-(12,8)
area = 50

The with statement

The with statement opens a record for the statements inside its block. Field names can then be written without the record variable. It shortens a long chain of accesses, but it also hides which variable is being changed, so it is best used for a short block with one obvious record.

withrec.pas
program withrec;
{$mode objfpc}{$H+}

type
  TPoint = record
    X, Y: Integer;
  end;

var
  p: TPoint;

begin
  with p do
  begin
    X := 7;
    Y := 9;
  end;
  WriteLn('p = (', p.X, ', ', p.Y, ')');
end.

Inside the block, X and Y refer to the fields of p. The same assignments could be written with the full p.X and p.Y. If the block declares a local variable with the same name as a field, the local variable hides the field, which is one reason many style guides restrict with to very short blocks.

$ fpc -gl withrec.pas
$ ./withrec
p = (7, 9)

Variant records

A variant record stores different field sets in the same memory, selected by a tag field. The declaration uses a case part inside the record. Only one variant is valid at a time, and the program is responsible for reading the variant that matches the tag. This is the Pascal equivalent of a tagged union.

variant.pas
program variant;
{$mode objfpc}{$H+}

type
  TKind = (kCircle, kRectangle);
  TShape = record
    Name: string;
    case Kind: TKind of
      kCircle: (Radius: Double);
      kRectangle: (Width, Height: Double);
  end;

var
  shape: TShape;

begin
  shape.Name := 'circle';
  shape.Kind := kCircle;
  shape.Radius := 2.5;
  WriteLn(shape.Name, ': radius = ', shape.Radius:0:1);
  WriteLn('SizeOf(TShape) = ', SizeOf(TShape));

  shape.Name := 'rectangle';
  shape.Kind := kRectangle;
  shape.Width := 3.0;
  shape.Height := 4.0;
  WriteLn(shape.Name, ': ', shape.Width:0:1, ' x ', shape.Height:0:1);
end.

The record is 32 bytes because the largest variant holds two doubles and the compiler aligns the whole record. After the tag changes to kRectangle, the Radius field must not be read; its bytes are now shared with Width. Variant records are compact and useful for file formats, but a class hierarchy or a discriminated union with explicit accessors is safer when the variants grow.

$ fpc -gl variant.pas
$ ./variant
circle: radius = 2.5
SizeOf(TShape) = 32
rectangle: 3.0 x 4.0

Packed records

The compiler inserts padding between fields so that each one starts at an address suitable for its type. packed record removes that padding and stores the fields as tightly as possible. The price is slower access on processors that dislike unaligned data, and the benefit is a smaller record or a layout that must match a binary format.

packedrec.pas
program packedrec;
{$mode objfpc}{$H+}

type
  TPacked = packed record
    Flag: Boolean;
    Value: Integer;
  end;
  TNormal = record
    Flag: Boolean;
    Value: Integer;
  end;

begin
  WriteLn('TPacked = ', SizeOf(TPacked));
  WriteLn('TNormal = ', SizeOf(TNormal));
end.

The normal record is eight bytes: the Boolean occupies one byte, three bytes of padding follow, and the integer starts at offset 4. The packed record is five bytes, with the integer immediately after the Boolean. A packed record is the right choice for reading a file header or a network packet, where the byte layout is defined by an external format.

$ fpc -gl packedrec.pas
$ ./packedrec
TPacked = 5
TNormal = 8

Managed fields

A record may contain strings, dynamic arrays, interfaces, and other managed types. The compiler generates hidden initialization and finalization code for such a record, and assigning one record to another copies the fields according to their own rules. Strings are copied on write, but a dynamic array field is a reference, so both records share the same array until one of them is resized or explicitly copied.

managed.pas
program managed;
{$mode objfpc}{$H+}

type
  TPerson = record
    Name: string;
    Tags: array of string;
  end;

var
  a, b: TPerson;

begin
  a.Name := 'Alice';
  a.Tags := ['admin', 'editor'];
  b := a;
  b.Name := 'Bob';
  b.Tags[0] := 'viewer';
  WriteLn('a.Name = ', a.Name);
  WriteLn('b.Name = ', b.Name);
  WriteLn('a.Tags[0] = ', a.Tags[0]);
  WriteLn('b.Tags[0] = ', b.Tags[0]);
end.

The name is copied, so a.Name stays Alice while b.Name becomes Bob. The tags array is shared, so changing b.Tags[0] also changes a.Tags[0]. When a record needs its own copy of an array field, assign a Copy of the array after the record assignment.

$ fpc -gl managed.pas
$ ./managed
a.Name = Alice
b.Name = Bob
a.Tags[0] = viewer
b.Tags[0] = viewer

Copying a managed array field

The next version makes the array independent. Copy(a.Tags, 0, Length(a.Tags)) creates a new array with the same elements, and the assignment stores that new array in b.

managed2.pas
program managed2;
{$mode objfpc}{$H+}

type
  TPerson = record
    Name: string;
    Tags: array of string;
  end;

var
  a, b: TPerson;

begin
  a.Name := 'Alice';
  a.Tags := ['admin', 'editor'];
  b := a;
  b.Tags := Copy(a.Tags, 0, Length(a.Tags));
  b.Tags[0] := 'viewer';
  WriteLn('a.Tags[0] = ', a.Tags[0]);
  WriteLn('b.Tags[0] = ', b.Tags[0]);
end.

Now a.Tags[0] remains admin and b.Tags[0] becomes viewer. The string field still behaves like a value because of copy-on-write, and the record still needs no manual cleanup. A record with managed fields cannot be written to a file with BlockWrite as raw bytes; the pointers, not the text, would be written.

$ fpc -gl managed2.pas
$ ./managed2
a.Tags[0] = admin
b.Tags[0] = viewer

Next steps

The advanced records article adds methods and operators to a record, and the classes article shows the reference type that adds inheritance and virtual methods. The arrays article explains the arrays that records often contain, and the pointers article shows how records are allocated on the heap.

Source

Free Pascal Reference guide

Free Pascal: records

Pascal on Wikipedia

In this article we have declared records, nested them, passed and returned them from functions, used with, stored variants in shared memory, packed a record for an external layout, and followed the rules for managed fields. The series continues with advanced records and operator overloading.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.