Reallocate + Clear = ReCallocate?
Reallocate + Clear = ReCallocate?
Could the developers consider adding a ReCallocate keyword to combine the functions of Reallocate and Clear?
Re: Reallocate + Clear = ReCallocate?
Clear as for 'Redim Preserve' on a byte array ?
Re: Reallocate + Clear = ReCallocate?
I really want a RAM buffer rather than an array. That's because I sometimes access bytes, and other times I access words or dwords, and even user defined types. I need that flexibility. Also, it's less confusing for me. I realise that a byte array would probably work, though.
Re: Reallocate + Clear = ReCallocate?
Why do you need the developers to add a new keyword? You could define it yourself, by creating a new function or a macro that calls both commands. The compiled code would be identical (and you wouldn't force every other programmer to include a piece of code that you would be the only one to use)
Re: Reallocate + Clear = ReCallocate?
I was working on a recallocate function before angros47's post, a bit stuck with clear
Note the 'byval' in 'clear' statement.
This function could/should be completed with testing null values.

Note the 'byval' in 'clear' statement.
This function could/should be completed with testing null values.
Code: Select all
function recallocate(ByVal ori As Any Ptr,ByVal cnt As UInteger ) As Any Ptr
dim as any ptr ptr_tempo
ptr_tempo=Reallocate(ori,cnt)
clear(byval ptr_tempo,0,cnt) ''byval is mandatory otherwise clearing the pointer value....
return ptr_tempo
end function
dim as any ptr anyptr = allocate(1000)
print anyptr
anyptr=recallocate(anyptr,2000)
print anyptr
deallocate(anyptr)
sleep
Re: Reallocate + Clear = ReCallocate?
I thought the author of this thread only wanted to clear memory additions (like 'Redim Preserve')
Re: Reallocate + Clear = ReCallocate?
Code: Select all
function recallocate(ByVal ori As Any Ptr,ByVal cntold As UInteger, ByVal cnt As UInteger ) As Any Ptr
dim as any ptr ptr_tempo1
dim as any ptr ptr_tempo2
ptr_tempo1=Reallocate(ori,cnt)
ptr_tempo2=ptr_tempo1+cntold
clear(byval ptr_tempo2,0,cnt-cntold) ''byval is mandatory otherwise clearing the pointer value....
return ptr_tempo1
end function
dim as any ptr anyptr = allocate(1000)
print anyptr
anyptr=recallocate(anyptr,1000,2000)
print anyptr
deallocate(anyptr)
sleep
Re: Reallocate + Clear = ReCallocate?
Does not work if cntold > cnt.
Re: Reallocate + Clear = ReCallocate?
The implication of your statement is that nobody uses Callocate.
My reason for using ReCallocate would the same reason that I use Callocate. In fact I see lots of examples in hard drive firmware where lazy programmers have not cleared RAM, leaving behind "noise".
Re: Reallocate + Clear = ReCallocate?
Thanks.SARG wrote: ↑Feb 13, 2023 10:20Code: Select all
function recallocate(ByVal ori As Any Ptr,ByVal cntold As UInteger, ByVal cnt As UInteger ) As Any Ptr dim as any ptr ptr_tempo1 dim as any ptr ptr_tempo2 ptr_tempo1=Reallocate(ori,cnt) ptr_tempo2=ptr_tempo1+cntold clear(byval ptr_tempo2,0,cnt-cntold) ''byval is mandatory otherwise clearing the pointer value.... return ptr_tempo1 end function dim as any ptr anyptr = allocate(1000) print anyptr anyptr=recallocate(anyptr,1000,2000) print anyptr deallocate(anyptr) sleep
Re: Reallocate + Clear = ReCallocate?
By using as members a private dynamic array and a public method, a Type structure for such a feature can be easily defined:
Code: Select all
Type AnyBuffer
Public:
Declare Function Recallocate(Byval size As Uinteger) As Any Ptr
Private:
Dim As Byte array(Any)
End Type
Function AnyBuffer.Recallocate(Byval size As Uinteger) As Any Ptr
If size > 0 then Redim Preserve This.array(size - 1) Else Erase This.array
Return @This.array(0)
End Function
'-----------------------------------------------------------------------------
Dim As AnyBuffer myBuffer1 '' buffer declaration for any data-type
Dim As Zstring Ptr pz '' pointer typing for user data-type
pz = myBuffer1.Recallocate(9+1) '' memory buffer allocation
pz[0] = "FreeBASIC" '' zstring data filling
Print "'" & *pz & "'"
Pz = myBuffer1.Recallocate(9+7+1) '' memory buffer reallocation
pz[9] = " 1.09.0" '' zstring data addition
Print "'" & *pz & "'"
pz = myBuffer1.Recallocate(0) '' memory buffer deallocation
Print "'" & *pz & "'"
Sleep
Code: Select all
'FreeBASIC'
'FreeBASIC 1.09.0'
''