I have read all of the documentation on using allocate/reallocate/deallocate for zstrings. I created a small test program to
test my knowledge. From the results of my test program I saw no benefits in using the allocate/reallocate/deallocate for zstrings.
I was under the assumption than if a zstring was initially set to a fixed size by using allocate and then resized to a smaller size
by using reallocate that the max size of the zstring would change to that size, from my testing (unless I am wrong in my testing code)
If a zstring is set to 128 and then the memory is reallocated to a smaller size and then copy the zstring to a dynamic string then the size of dynamic
string is changed but the data for the string is still there except a null (0) will be at the index location inside that string. So is there a
way to resize a zstring that is smaller to that size when copied to a dynamic string?
Code: Select all
Dim SymbolTbl(0 to 6) as string
Dim Buffer as zstring ptr
Buffer = cAllocate(128)
dim index as byte
dim strh as string
Dim Buffer2 as zstring*128
'buffer should be 128 bytes long
SymbolTbl(1) = *Buffer
Print "len(SymbolTbl(1)) = ";len(SymbolTbl(1))
print "sizeof (SymbolTbl(1)) = ";sizeof(SymbolTbl(1))
*Buffer = "myva"
Print "len(*Buffer) = ";len(*Buffer)
print "sizeof (*Buffer) = ";sizeof(*Buffer)
Buffer = reallocate(Buffer,9)
SymbolTbl(0) = *Buffer
Print "len(SymbolTbl(0)) = ";len(SymbolTbl(0))
print "sizeof (SymbolTbl(0)) = ";sizeof(SymbolTbl(0))
*Buffer = "myvar equ"
SymbolTbl(2) = *Buffer
strh = SymbolTbl(2)
Print "len(SymbolTbl(2)) = ";len(SymbolTbl(2))
print "sizeof (SymbolTbl(2)) = ";sizeof(SymbolTbl(2))
for Index = 0 to 16
print "SymbolTbl(2) char "; Index ; " ";strh[index]
next
'now do the same thing without allocate/deallocate
'buffer should be 128 bytes long
SymbolTbl(5) = Buffer2
Print "len(SymbolTbl(5)) = ";len(SymbolTbl(5))
print "sizeof (SymbolTbl(5)) = ";sizeof(SymbolTbl(5))
Buffer2 = "myva"
Print "len(Buffer2) = ";len(Buffer2)
print "sizeof (Buffer2) = ";sizeof(Buffer2)
SymbolTbl(4) = Buffer2
Print "len(SymbolTbl(4)) = ";len(SymbolTbl(4))
print "sizeof (SymbolTbl(4)) = ";sizeof(SymbolTbl(4))
Buffer2 = "myvar equ"
SymbolTbl(6) = Buffer2
strh = SymbolTbl(6)
Print "len(SymbolTbl(6)) = ";len(SymbolTbl(6))
print "sizeof (SymbolTbl(6)) = ";sizeof(SymbolTbl(6))
for Index = 0 to 16
print "SymbolTbl(6) char "; Index ; " ";strh[index]
next
Deallocate(buffer)