t3x.org / t3x / t3x-manual / 243.html
 
T3X - A Minimum Procedural Language
Version 8.1.5, Online Edition
Copyright(C) 1996-2004
Nils M Holm
 
Previous:
2.4.2 Constants
TOC | Index | Back Next:
2.4.4 Structures

2.4.3 Vectors

Vectors are compile time variables, too. When they are declared, they will be initialized with the address of an array of subsequent machine words, the so-called vector members or vector elements. The address of a vector is equal to the address of its first member. Any number of vectors may be defined in a single VAR statement. Declarations of vectors and atomic variables may be mixed in one and the same statement:

VAR RingBuffer[1000], Head, Tail;

Vector declarations differ from atomic variable declarations by the trailing square brackets containing a constant expression which specifies the size of the vector in machine words. The first member of a vector has an index value of 0 and the last one has an index of vectorsize-1 (999 in the above example). The maximum size of a vector may be limited by an environmental restriction.

Since vector addresses are stored in compile time variables, they may not change at run time. It is legal to change the values of vector members, though. When occurring in righthand side expressions, vector names evaluate to the addresses of their associated arrays.

Single members of a vector may be addressed using the subscript operator []. The expression

v[5]

for example, evaluates to the fifth member of the vector v (given that the first member of the vector is actually referred to as the zeroe'th member). Subscripted vectors may occur on the left sides of expressions, as well. The assignment

v[i] := 99;

would change the i'th member of v to 99. Like atomic variables, the members of vectors may be used to store any data type, even pointers to vectors. See the description of the subscript operators for details about nested vectors.

A special case of the vector is the byte vector. Like `ordinary' vectors, they are declared in VAR statements:

VAR Input::256, Output::256;

The only difference between a vector and a byte vector is the computation of the required size. The size value after the ::-operator specifies the number of characters required. The amount of memory actually allocated depends on the size of a machine word on the target machine, which is returned by the core class procedure T3X.BPW() (the method BPW of the class T3X). The size of a byte vector is computed using this formula (T being an instance of the T3X class):

vectorsize + T.BPW() - 1
------------------------
         T.BPW()

It allocates enough space for at least vectorsize characters. No further type information is associated with vectors. Therefore, it is valid to access byte vector members using [] and word vector members using ::. However, both of these operations are discouraged, because the actual vector sizes depends on a specific implementation and alignment errors may occur at runtime.

Previous:
2.4.2 Constants
TOC | Index | Back Next:
2.4.4 Structures