Reallocate + Clear = ReCallocate?

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Reallocate + Clear = ReCallocate?

Post by fzabkar »

Could the developers consider adding a ReCallocate keyword to combine the functions of Reallocate and Clear?
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reallocate + Clear = ReCallocate?

Post by fxm »

Clear as for 'Redim Preserve' on a byte array ?
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Reallocate + Clear = ReCallocate?

Post by fzabkar »

fxm wrote: Feb 13, 2023 5:50 Clear as for 'Redim Preserve' on a byte array ?
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.
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: Reallocate + Clear = ReCallocate?

Post by angros47 »

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)
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Reallocate + Clear = ReCallocate?

Post by SARG »

I was working on a recallocate function before angros47's post, a bit stuck with clear :D

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
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reallocate + Clear = ReCallocate?

Post by fxm »

I thought the author of this thread only wanted to clear memory additions (like 'Redim Preserve')
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Reallocate + Clear = ReCallocate?

Post by SARG »

fxm wrote: Feb 13, 2023 10:04 I thought the author of this thread only wanted to clear memory additions (like 'Redim Preserve')

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
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reallocate + Clear = ReCallocate?

Post by fxm »

Does not work if cntold > cnt.
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Reallocate + Clear = ReCallocate?

Post by SARG »

fxm wrote: Feb 13, 2023 10:45 Does not work if cntold > cnt.
I know but always let a bit of work to the others :lol:
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Reallocate + Clear = ReCallocate?

Post by fzabkar »

angros47 wrote: Feb 13, 2023 8:38(and you wouldn't force every other programmer to include a piece of code that you would be the only one to use)
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".
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Reallocate + Clear = ReCallocate?

Post by fzabkar »

SARG wrote: Feb 13, 2023 10:20
fxm wrote: Feb 13, 2023 10:04 I thought the author of this thread only wanted to clear memory additions (like 'Redim Preserve')

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
Thanks.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Reallocate + Clear = ReCallocate?

Post by fxm »

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'
''
Post Reply