Sizeof and Countof

Top  Previous  Next

Sizeof (Tibbo C)

Tibbo C features the standard implementation of the sizeof() operator.

 

Sizeof (Tibbo BASIC)

Tibbo BASIC implements the sizeof() operator but in BASIC it can only be used on types, i.e. Tibbo BASIC structure definitions:

 

Tibbo BASIC:

type t1_type
  a as char
  i as integer
end type
 
dim t1 as t1_type
 
sub on_sys_init
  dim i as word
  i=sizeof(t1_type)   'i will be 3 (char + integer)
  i=sizeof(t1)        'this will generate error
end sub

 

Countof (Tibbo BASIC only)

The countof() operator in BASIC can be used to get the number of elements in arrays and structures.

It also returns the string capacity for non-array string variables.

The following example illustrates how the countof() operator behaves.

 

Tibbo BASIC:

dim l as dword
dim x(5) as byte
dim y(5,2) as word
 
dim s as string(20)     'a string variable with the capacity for 20 characters
dim ss(10) as string(20) 'an array of 10 string variables with the capacity of 20 characters each
 
type t1_type
  a as char
  i as integer
end type
 
dim t1 as t1_type
 
sub on_sys_init
  dim i as word
  i=countof(i)       'i will be 1 -- this dword variable occupies 4 bytes but countof() returns the number of members, not the size in memory
  i=countof(x)       'i will be 5 -- the number of elements in the x array
  i=countof(y)       'i will still be 5 -- the countof() operator can only see into the first array dimension
  i=countof(s)       'i will be 20 -- the string's capacity
  i=countof(ss)     'i will be 10 -- the number of array members; the countof() operator will not be able to see beyond this
  i=countof(t1)     'i will be 2 -- t1 has two members
  i=countof(t1_type) 'this will generate error
end sub