Node:Subrange Types, Next:, Up:Type Definition Possibilities



Subrange Types

GNU Pascal supports Standard Pascal's subrange types:

program SubrangeDemo;
type
  MonthInt = 1 .. 12;
  Capital = 'A' .. 'Z';
  ControlChar = ^A .. ^Z;  { `^A' = `Chr (1)' is a BP extension }
begin
end.
Also possible: Subranges of enumerated types:
program EnumSubrangeDemo;
type
  { This is an enumerated type. }
  Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

  { This is a subrange of `Days'. }
  Working = Mon .. Fri;

begin
end.

To increase performance, variables of such a type are aligned in a way which makes them fastest to access by the CPU. As a result, 1 .. 12 occupies 4 bytes of storage on an IA32 CPU.

For the case you want to save storage at the expense of speed, GPC provides a packed variant of these as an extension:

program PackedSubrangeDemo;
type
  MonthInt = packed 1 .. 12;
begin
end.

A variable of this type occupies the shortest possible (i.e., addressable) space in memory - one byte on an IA32 compatible CPU.

See also: packed.