CreateBank ( [size] )

Parameters

size (optional) - size of the bank in bytes; 0 (default)

Description

Creates a memory bank - a raw block of bytes you can read and write at any offset.

Banks are the tool for binary data: building custom file formats (load a whole file with ReadBytes, pick it apart with the Peeks), packing network packets, passing structures to DLLs with CallDLL, or just storing lots of values more compactly than arrays allow. A new bank starts filled with zeros, and offsets run from 0 to BankSize-1.

The value types you can Peek and Poke are:

Byte : 1 byte, values 0 to 255
Short : 2 bytes, values 0 to 65535
Int : 4 bytes, values -2147483648 to 2147483647
Float : 4 bytes

Values can sit at any byte offset - no alignment rules - but every access is range checked, so peeking or poking outside the bank raises a runtime error instead of corrupting memory.

In Extended mode there are also Long and Double variants (8 bytes each); see the wide numbers reference.

See also: FreeBank, BankSize, ResizeBank, PokeByte, ReadBytes.

Example

; CreateBank Example
; ------------------

; A player save-game record packed into a memory bank.
; Field sizes: byte=1, short=2, int=4, float=4 bytes.
;
;   offset 0   flags   (byte,  1 byte )
;   offset 1   hp      (short, 2 bytes)
;   offset 3   score   (int,   4 bytes)
;   offset 7   x_pos   (float, 4 bytes)   = 11 bytes in total

; CreateBank reserves an 11 byte block of memory for the record
save=CreateBank(11)

; Pack the record - each Poke writes its field at a byte offset
PokeByte save,0,5
PokeShort save,1,250
PokeInt save,3,1234567
PokeFloat save,7,17.25

; Unpack the record - each Peek reads its field back
Print "Player record in a "+BankSize(save)+" byte bank:"
Print ""
Print "offset 0   flags (byte)  = "+PeekByte(save,0)
Print "offset 1   hp    (short) = "+PeekShort(save,1)
Print "offset 3   score (int)   = "+PeekInt(save,3)
Print "offset 7   x_pos (float) = "+PeekFloat(save,7)

; Dump every byte so the layout is visible
; (ints and floats are stored low byte first)
dump$=""
For i=0 To BankSize(save)-1
    dump$=dump$+PeekByte(save,i)+" "
Next
Print ""
Print "Raw bytes: "+dump$

; Release the memory when done
FreeBank save

Print ""
Print "Press any key to close the example"
WaitKey

End

Index