Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Pascal/HP-UX Programmer's Guide > Chapter 5 Allocation and Alignment

Records

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

A record allocation is the sum of the allocations of the fields in the fixed part and (if the record has them) the allocations of the tag field and the largest field in the variant part, plus trailing bits.

Field allocation depends on field type and whether the record is unpacked, packed, or crunched. The same factors determine field alignment. See the tables indicated below:

 

The HP Pascal packing algorithm uses these two rules to align a record:

  • The entire record is aligned on the same boundary as its most restricted field.

  • The variant part of a record is aligned on the same boundary as the most restricted first field of all variants.

Example

TYPE
Rec = RECORD
CASE b : Boolean OF
TRUE : (c : char; {1 byte, 1-byte-aligned}
l : longreal; {8 bytes, 8-byte-aligned}
);
FALSE : (i : integer; {4 bytes, 4-byte-aligned}
);
END;

A record of the type Rec is 8-byte-aligned because its most restricted field, l, must be 8-byte-aligned.

The variant part of a record of type Rec is 4-byte-aligned, because the most restricted first field of the two variants, i, must be 4-byte-aligned.

A variable of type Rec is allocated 16 bytes. The TRUE and FALSE variants are aligned like this:

Figure 5-6 TRUE Variant

TRUE Variant

Figure 5-7 FALSE Variant

FALSE Variant

Sometimes you can reduce the space that a record takes by declaring its fields in different order.

Example

VAR
upr1 : RECORD
bf : Boolean;
pf : 0..32767;
cf : char;
END;

upr2 : RECORD
bf : Boolean;
cf : char;
pf : 0..32767;
END;

The only difference between the variables upr1 and upr2 is the order of their fields.

The variable upr1 takes six bytes:

Figure 5-8 Records Example 1

Records Example 1

Because pf must be 2-byte-aligned, it cannot start in the second byte. The extra byte after cf is allocated because the most restricted element, pf, is 2-byte-aligned.

The variable upr2 takes four bytes:

Figure 5-9 Records Example 2

Records Example 2

Sometimes you cannot reduce the space that a record takes by declaring its fields in different order.

Example

VAR
pr1 : PACKED RECORD
srf : 0..32;
b : Boolean;
pf : 0..32767;
cf : char;
END;

pr2 : PACKED RECORD
srf : 0..32;
b : Boolean;
cf : char;
pf : 0..32767;
END;

The only difference between the variables pr1 and pr2 is the order of their fields.

The variable pr1 takes four bytes:

Figure 5-10 Records Example 3

Records Example 3

The variable pr2 also takes four bytes:

Figure 5-11 Records Example 4

Records Example 4
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.