• Aucun résultat trouvé

Internal Representation of Arrays

Dans le document Domain Pascal Language Reference (Page 100-104)

QUAD WORD ALIGNMENT

3.11.4 Internal Representation of Arrays

Packed arrays usually require less storage space than arrays that are not packed. In this section we describe how both types of arrays are represented internally.

3.11.4.1 Non-Packed Arrays

With two exceptions. the total amount of memory required to store an array that is not packed equals the number of elements in the array times the amount of space required to store one element. The amount of space for one element depends on the component type of the array. as shown in Table 3-4.

Data Types 3-45

Table 3-4. Size of One Element of an Array

Base Data Type Size of One Element

Integer 16 or Integer 16 bits

Integer32 32 bits

Single or Real 32 bits

Double 64 bits

Boolean 8 bits

Char 8 bits

Subrange size of base type of subrange

Enumerated 16 bits

The two exceptions to this rule are arrays of booleans and varying arrays of chars.

If the component type of an array is boolean and an odd number of elements are de-clared, the compiler adds an extra pad byte to the storage space for the array so that the storage is an even number of bytes. For example, if you declare boolean array b as VAR

b : array[1 .. 5] of boolean;

Domain Pascal reserves six bytes of memory for b.

A variable-length string always begins with a 2-byte length field, followed by the body field, followed by padding. The body field must always be capable of holding a null char-acter at the position just beyond the current length. Consequently, the compiler always allocates to the body field one byte more than the maximum length specified in the decla-ration. The compiler adds 2 or 3 bytes of padding to make the total size of the string an even number of longwords (4 bytes).

In summary, to figure the total number of bytes allocated for a variable-length string: add either 5 or 6 bytes to the number of bytes specified as the maximum length for the string such that the total number of bytes is an even number. For instance:

VAR

v2: VARYING [2] OF CHAR;

v7: VARYING [7] OF CHAR;

vB: VARYING[B] OF CHAR;

Multidimensional Arrays

{B bytes are allocated}

{12 bytes are allocated}

{14 bytes are allocated}

Multidimensional arrays are stored in row major order. Given a 2-dimensional array of the following declaration:

a : array[1 .. 2, 1 .. 3] of integer16;

3-46 Data Types

Domain Pascal represents it in the following order:

a[l,l] first a[l,2] second a[l,3] third a[2,l] fourth a[2,2] fifth a[2,3] sixth

3.11.4.2 Packed Arrays

Table 3-5 shows the space required for each type of element in a packed array.

Table 3-5. Storage of Packed Array Elements

Type of Element Space Allocation

Subranges of integers Exact number of bits required .;

Enumerated bit aligned ••

Subrange of enumerated

Integer 16 bits; word aligned

Integer16

Boolean 1 bit; bit aligned

Real 32 bits; word aligned

Integer32 Pointer

Double 64 bits; word aligned

Character 8 bits; byte aligned

Subrange of character

Character array Exact number of bytes required; byte aligned

Record Exact number of words required; word

Array of non-characters aligned

Set Exact number of bits required 1;

bit aligned 2

1 If the element size is less than 16 bits, the element is padded up to the nearest power of 2 bits.

2 If the element size is greater than 16 bits, then the elements are word aligned.

In packed arrays, if the element size is less than 16 bits, then the element is padded up to the nearest power of 2 bits. Thus, in the following example, 4 bits would be used to store each element of arrayl and 2 bytes would be used to store the entire arrayl:

array1: PACKED ARRAY [1 .. 3] OF 0 .. 7

Data Types 3-47

I

I

Contrast this to the following declaration:

array2: ARRAY [1 .. 3] OF 0 .. 7

Each element of arrayl requires 16 bits of storage, and 48 bits would be used to store the entire arrayl.

3.12 Files

When you open a file for I/O access, you must specify a file variable that will be the pseu-donym for the actual pathname of the file. Thereafter, you specify the file variable (not the pathname) to refer to the file. Domain Pascal supports the file data type and the text data type. (Throughout this manual, the word "file," in boldface type, means the file data type, and the word "file," in roman type, means a disk file.) Files of both file type and text type are stored as Domain unstruct (unstructured ISO Latin-1) files. These files are compatible with text . files produced under UNIX systems.

The following declaration establishes variable fl as an identifier of a text file:

VAH

fl : text;

A text file contains sequences of ISO Latin-1 characters representing variable-length lines of text. You can read or write entire lines of a text file. You can read from or write to a text file the values of a variable of any type (except pointer and file). Chapter 8 describes text files in more detail.

You specify a file variable with the following format:

variable: file of base_type;

A variable with the file type specifies an unstruct binary file composed of values having the base_type. That is, the only permissible values in such a file all have the same data type, that of the base_type. The base_type can be any type except a pointer, file, or text type. For example, the following declaration creates a file type corresponding to a file that consists entirely of student records:

TYPE

student record

name array [1 .. 30] of char;

id integer32 end;

U_of_M FILE OF student;

The Domain/OS operating system stores each occurrence of student in 34 bytes: 30 bytes for name and 4 bytes for id.

3-48 Data Types

NOTE: Older versions of Domain Pascal created special record struc-tured Domain files (called "rec" files) when you opened a file with file type. For compatibility with older versions, the current version of Domain Pascal allows you to manipulate rec files, but you cannot create them. When you open an existing file with the file type, Domain Pascal checks whether it is a rec or unstruct file, and accesses it appropriately. Whenever you open a new file, however, Domain Pascal creates an unstruct file.

3.13 Pointers

A pointer variable points to a dynamic variable. In Domain Pascal, the value of a pointer variable is a variable's virtual address. Domain Pascal supports the pointer type declaration of standard Pascal as well as a special univ _ptr data type and procedure and function pointer types. This section details the declaration of pointer types. You should also refer to the "Pointer Operations" listing of Chapter 4 for information on using pointers in your pro-grams.

Dans le document Domain Pascal Language Reference (Page 100-104)