Hi, I spend my free time learning programming in FreeBASIC. However, I'm having some trouble with the Allocate and Callocate functions. No matter what value I enter into the size/count parameters, it only allocates 8 bytes of memory. For example, this code should get stuck in an infinite loop:
Code: Select all
Dim As UByte Ptr bArray
bArray = Callocate(3,1) 'Allocate 3 bytes
Loop Until Len(bArray) = 8 'Exit loop if 8 bytes allocated
For i As ULong = 0 To Len(bArray)-1 Step 1
bArray[i] = i + 3
But it doesn't. I have no idea what could be causing this. Is this a bug? Am I doing something wrong? Does anyone else have this problem?
There are several things going wrong in that code.
does (unfortunately) not return the size of the array. In returns the size in bytes of a pointer (4 for 32-bit fbc, 8 for 64-bit fbc).
B) Do : bArray = Callocate(3,1) : Loop Until Len(bArray) = 8
is how you create a memory leak. You ask the computer 3 bytes of memory each time, but on next loop you overwrite the pointer that points to the memory and the location of the previous 3 bytes is lost.
You can increase the size of an allocated memory block with Reallocate
. But it is unclear to me what you actually want to do.