Memory

Allocating memory blocks and reading and writing memory directly.

Memory comes from the operating system's process heap. Blocks are not freed automatically: free each one with FreeMemory when it is no longer needed.

AllocateMemory ✅

*Memory = AllocateMemory(Size [, Flags])

Allocates a block of Size bytes, filled with zeros.

Parameter Meaning
Size bytes to allocate
Flags ignored for now (PureBasic's #PB_Memory_NoClear skips the zero fill)

Returns the address of the block, or 0 if there isn't enough memory.

OpenConsole()
*buf = AllocateMemory(16)
PokeL(*buf, 1000)
PokeL(*buf + 4, 234)
PrintN(Str(PeekL(*buf) + PeekL(*buf + 4)) + " " + Str(PeekL(*buf + 8)))
FreeMemory(*buf)

Output:

1234 0

ReAllocateMemory ✅

*NewMemory = ReAllocateMemory(*Memory, Size [, Flags])

Changes the size of a block. The contents are kept, up to the smaller of the old and new sizes. Any new bytes are zero. The block may move, so use the returned address from then on.

If *Memory is 0, this allocates a new block.

Returns the address of the block, or 0 if there isn't enough memory (the old block is then unchanged).

FreeMemory ✅

FreeMemory(*Memory)

Frees a block from AllocateMemory or ReAllocateMemory. Freeing 0 does nothing.

MemorySize ✅

Size = MemorySize(*Memory)

Returns the size of a block from AllocateMemory or ReAllocateMemory (as asked for), or 0 for 0.

CopyMemory ✅

CopyMemory(*Source, *Destination, Size)

Copies Size bytes. The two areas may overlap.

OpenConsole()
*a = AllocateMemory(8)
*b = AllocateMemory(8)
PokeQ(*a, 123456789)
CopyMemory(*a, *b, 8)
PrintN(Str(PeekQ(*b)))
FreeMemory(*a)
FreeMemory(*b)

Output:

123456789

FillMemory 🟡

FillMemory(*Memory, Size [, Value [, Type]])

Sets Size bytes to Value (default 0).

Notes. For now every fill is byte by byte. Type (word, long or integer fill) is accepted but ignored.

Peek and Poke 🟡

Value = PeekL(*Address)
PokeL(*Address, Value)

Read or write one value of a given type at an address. The compiler turns each of these into a single instruction.

Suffix Type Size
A ubyte (Ascii) 1
B byte 1
C char 2
W word 2
U uword (Unicode) 2
L long 4
I int 8
Q quad 8
F float 4
D double 8

For example PeekB, PokeU, PeekD. Reads of signed types are sign-extended.

OpenConsole()
*m = AllocateMemory(8)
PokeB(*m, -1)
PokeD(*m + 1, 0.5)
PrintN(Str(PeekB(*m)) + " " + Str(PeekA(*m)) + " " + StrD(PeekD(*m + 1)))
FreeMemory(*m)

Output:

-1 255 0.5

Strings: see PeekS and PokeS below.

CopyStructure ✅

CopyStructure(*Source, *Destination, Structure)

Copies a structure value from one address to another. Strings inside are shared with the copy (as with =), and any strings that were in the destination are released.

Structure Item
  name.s
  count.i
EndStructure

Define a.Item, b.Item
a\name = "bolts"
a\count = 40
CopyStructure(@a, @b, Item)
b\count + 2

OpenConsole()
PrintN(b\name + " " + Str(b\count) + " " + Str(a\count))

Output:

bolts 42 40

AllocateStructure, FreeStructure ✅

*Item.Structure = AllocateStructure(Structure)
FreeStructure(*Item)

AllocateStructure allocates a new structure: numbers 0, strings empty, and its lists, maps and arrays made and empty. Returns its address (typed, so *p = AllocateStructure(Person) works with *p\name$), or 0 if there isn't enough memory.

FreeStructure frees its strings, lists, maps and arrays, then the structure. FreeStructure(0) does nothing, and an address that AllocateStructure didn't make is left alone, as far as x can tell.

OpenConsole()
Structure Person
  name$
  List pets.s()
EndStructure
*p.Person = AllocateStructure(Person)
*p\name$ = "Ann"
AddElement(*p\pets()) : *p\pets() = "cat"
PrintN(*p\name$ + " has a " + *p\pets())
FreeStructure(*p)

Output:

Ann has a cat

InitializeStructure, ClearStructure, ResetStructure ✅

InitializeStructure(*Memory, Structure)
ClearStructure(*Memory, Structure)
ResetStructure(*Memory, Structure)

For a structure in memory from elsewhere (AllocateMemory, a buffer):

  • InitializeStructure makes its lists, maps and arrays. The memory should be zeros, as AllocateMemory gives.
  • ClearStructure frees its strings, lists, maps and arrays (and sets them to 0). Its numbers are kept. The memory itself isn't freed.
  • ResetStructure clears it, fills it with zeros and initializes it again: a fresh structure.
OpenConsole()
Structure Person
  name$
  age.i
  List pets.s()
EndStructure
*m.Person = AllocateMemory(SizeOf(Person))
InitializeStructure(*m, Person)
*m\name$ = "Bo" : *m\age = 7
AddElement(*m\pets()) : *m\pets() = "fish"
PrintN(*m\name$ + " " + *m\pets())
ResetStructure(*m, Person)
PrintN("[" + *m\name$ + "] " + Str(*m\age) + " " + Str(ListSize(*m\pets())))
ClearStructure(*m, Person)
FreeMemory(*m)

Output:

Bo fish
[] 0 0

MoveMemory ✅

MoveMemory(*Source, *Destination, Length)

Copies Length bytes, as CopyMemory does, when the two blocks may overlap.

OpenConsole()
*m = AllocateMemory(16)
PokeS(*m, "abcdefgh", -1, #PB_Ascii)
MoveMemory(*m, *m + 2, 4)
PrintN(PeekS(*m, -1, #PB_Ascii))

Output:

ababcdgh

MemoryStringLength ✅

Result = MemoryStringLength(*String [, Flags])

The length of a zero-terminated string in memory, in characters. Flags: #PB_Unicode (the default), #PB_Ascii, #PB_UTF8; with | #PB_ByteLength, in bytes (UTF-8 characters can take several).

PeekS ✅

Text$ = PeekS(*Memory [, Length [, Format]])

Reads a string from memory: up to Length characters, or up to a 0 if Length is −1 or left out. Format is #PB_Unicode (UTF-16, the default), #PB_UTF8 or #PB_Ascii.

PokeS ✅

Result = PokeS(*Memory, Text$ [, Length [, Flags]])

Writes a string to memory, followed by a 0. Length limits the characters written. Flags is the format, as for PeekS, optionally combined with #PB_String_NoZero to leave out the 0.

Returns the number of bytes written, not counting the 0.

OpenConsole()
*m = AllocateMemory(64)
n = PokeS(*m, "Grüße", -1, #PB_UTF8)
PrintN(Str(n) + " bytes: " + PeekS(*m, -1, #PB_UTF8))
PokeS(*m, "wide")
PrintN(PeekS(*m) + " " + PeekS(*m, 2))
FreeMemory(*m)

Output:

7 bytes: Grüße
wide wi

CompareMemory ✅

Result = CompareMemory(*Memory1, *Memory2, Size)

Returns 1 if the Size bytes at the two addresses are the same, otherwise 0.

OpenConsole()
*a = Ascii("hello") : *b = Ascii("help!")
PrintN(Str(CompareMemory(*a, *b, 3)) + Str(CompareMemory(*a, *b, 4)))

Output:

10

Not yet available 🔜

Nothing for now.