*grinds teeth*
No offense, but that's a really bad way to write code, mainly because dynamic strings are pretty inefficient (that is, when they're actually used dynamically - it's OK to use dynamic strings, but if the size of the string is changing constantly it's probably better to do it some other way). Static is always faster, no matter what variable type you use.
Far preferable would be to use OOP... have you heard of RAII? cha0s did a tutorial on it once. RAII says that you do all the allocation in the constructor, and all deallocation in the destructor of the object. Both of those are called automatically, so there's no need to worry about allocating/deallocating the memory in your program. The object does that all by itself. Then you just have to worry about it being dynamic - that's pretty simple too. You can overload operators or just use member functions to add or remove items in your object. The object itself keeps track of whether anything needs to be allocated or deallocated. It's very nice really.
Of course, it's your choice... but it's a lot harder to do what you're doing that way.
By the way, if you want to store integers in a string, here's one way to do it (implemented like a stack):
Code: Select all
Sub push_integer(ByRef mystr As String, myint As Integer)
Dim As Byte Ptr myb = CPtr(Byte Ptr, @myint)
mystr = mystr + Chr(myb[0])
mystr = mystr + Chr(myb[1])
mystr = mystr + Chr(myb[2])
mystr = mystr + Chr(myb[3])
End Sub
Function pop_integer(ByRef mystr As String) As Integer
Dim As Integer myint
Dim As Integer lms = Len(mystr)
Dim As Byte Ptr myb = CPtr(Byte Ptr, @myint)
If Len(mystr) = 0 Then Return 0
myb[0] = mystr[lms-4]
myb[1] = mystr[lms-3]
myb[2] = mystr[lms-2]
myb[3] = mystr[lms-1]
mystr = Left(mystr, lms-4)
Return myint
End Function
Dim As String mystr
push_integer(mystr, 1)
push_integer(mystr, 2)
push_integer(mystr, 3)
Print Str(pop_integer(mystr))
Print Str(pop_integer(mystr))
Print Str(pop_integer(mystr))
Print Str(pop_integer(mystr))
Sleep
I do not recommend coding this way, but there's a solution if you want to. You can replace integer here with uinteger or single (for double you need 8 bytes instead of 4) - I assume you can figure out what to do with all this.