Redim is a core macro for creating dynamic, resizable arrays, and this is an enhancement to provide immediate deallocation of string and bstring elements when the array is down-sized.
This version will be included in the next o2 update:
def redim
=========
'
'redim %1 %2(%3)
'%1 type of array
'%2 name of array
'%3 number of elements
'
#ifndef %2_buffer
dim static string %2_buffer = ""
dim static int %2_count
dim static %1 byref %2
#endif
%2_count=%3
scope
dim sys %2_qn = len(%2_buffer) / sizeof(%2)
if %2_count > %2_qn 'EXTEND ARRAY
%2_buffer += nuls( sizeof(%2) * (%2_count - %2_qn) )
elseif %2_count < %2_qn 'REDUCE ARRAY
#if (typecodeof(%2)>0xc0)and(typecodeof(%2)<0x100)
string *%2_str : @%2_str = @%2
'DISPOSE OF BSTRING AND STRING ELEMENTS
do
if %2_qn<=%2_count then exit do
%2_qn--
freememory strptr %2_str
strptr(%2_str) = 0
@%2_str+=sizeof sys
loop
#endif
%2_buffer = left( %2_buffer, sizeof(%2) * %2_count )
end if
@%2=strptr %2_buffer
end scope
end def
'
'TESTS:
=======
int n=1000
redim string s(n)
s(100)="ABC"
s(101)="abc"
print s(101)
redim string s(200)
'print s(100)
function f()
n=100
redim string s(n)
print s[100]
s[100]="DEF"
end function
'
f()
print s[100]
print "ok"