Page 1 of 1

[Implemented] ClearMemory(*buffer,size)

Posted: Fri Nov 23, 2007 4:58 pm
by Rook Zimbabwe
I have been twiddling with the peek and poke series and think PB needs an easy and efficent way to CLEAR a *buffer memory. :D

Posted: Fri Nov 23, 2007 7:17 pm
by Thalius
If you dont want to allocate new, you could probably use this:

Using Longs is prolly faster than using bytes, tho not sure how its with data. This is basically what i came up with in 10 Mins ;)

EDIT: lol damn too slow post =P as i see in Coding Questions

Code: Select all

; Clear Membuffer (MAKE SURE SIZE IS NOT LARGER THAN BUFFER!)
 Procedure ClearMemory(*Buffer,Size.l)
  Protected *cptr.Long = *Buffer
  
  If *Buffer And Size
    For i = 0 To Size Step 4
      *cptr\l = 0 : *cptr + i
      Debug i
    Next
    ProcedureReturn 1
  EndIf
  
  ProcedureReturn 0
 EndProcedure

; Struct for our Memory
Structure _a
  x.l
  y.l
EndStructure

#BufferSize = 128

*mem = AllocateMemory(#BufferSize)

*ptr._a = *mem
; Fill in Values
*ptr\x = 5
*ptr\y = 8

; Check Values with PeekL
Debug PeekL(*ptr)
Debug PeekL(*ptr+SizeOf(_a\x))

; Clear
Debug "Clear: "+Str(ClearMemory(*ptr,#BufferSize))

; Check Values with PeekL
Debug PeekL(*ptr)
Debug PeekL(*ptr+SizeOf(_a\x))
Thalius