.SIZEOF

Updated May 2019; see History

.SIZEOF(variable)

.SIZEOF evaluates at compile-time to the size in bytes of the specified variable or defined structure, which must have been previously defined with a MAP or DEFSTRUCT statement. For example:

defstruct ST_REC

    map2 field1,s,10

    map2 field2,f

endstruct

map1 price,f,8

map1 vs$,s,0                    

map1 rec, ST_REC

map1 rec'copy, x, sizeof(ST_REC) ! rec'copy, x, 16

dimx xary(5), x, .sizeof(rec)    ! xary(5), x, 16

map1 fbytes(.sizeof(price)),b,1  ! fbytes(8),b,1

map1 ary(10)

    map2 foo,f

    map2 bar,i,2

print .sizeof(ST_REC)         ! 16

print .sizeof(rec)            ! 16

print .sizeof(rec'copy)       ! 16

print .sizeof(ST_REC.field2)  ! 6

print .sizeof(rec.field1)     ! 10

print .sizeof(vs$)            ! 16 (see comments)

print .sizeof(ary(1))         ! 8

print .sizeof(foo(1))         ! 6

 

Comments

Although most of the examples above show use of the .sizeof() compiler function in a PRINT statement, where it might be confused with a run-time function, in all cases, the function is evaluated by the compiler. It is converted to a fixed integer at the point it is first encountered, and thus may be used anywhere a literal integer is otherwise required, such as when declaring the size of a variable or the number of elements in an array.

The .sizeof function may take as an argument the name of a defined structure (ST_REC), the name of a field within the defined structure (ST_REC.field2), the name of an ordinary mapped variable (price), an instance of a structure (rec), a field within an instance of a structure (rec.field1), or an array element (ary(1) or foo(1)).

Variable length S and X variables, such as vs$ in the example above, always return a size of 16; this is the internal size of the descriptor for the variable, not the size of the data held by the variable, which changes at runtime and which can be accessed via the run-time len(vs$) function.

Use of .sizeof when defining related variables is a good technique for avoiding inadvertent errors that arise when you later change the size of one of the variables and forget to change the other. Another way to reduce that risk is to use the ST_REC and ary(1) in the example above.

Because of the commonality of the sizeof(var) compiler function across many languages, sizeof (without the dot) is treated as equivalent to .sizeof (with the dot). That is not normally the case with Dot Functions, where the dot was used to avoid conflicts with user array variables or procedures of the same name.

History

2019 May, A-Shell 6.5.1661:  .SIZEOF() now supports DEFTYPE names, e.g. .SIZEOF(BOOLEAN).