Memory Block

BlitzMax Forums/BlitzMax Programming/Memory Block

Hello,

Does anyone know if I can allocate a large block of memory, referenced by pointer. Then I can write Ints and Floats, etc into arbitary points within this block? I havent had any time to test this out myself or look it up because I havent been at my BMX computer for days.

Im asking this because im writing a small interprative emulator for my own virtual cpu and I need this for both speed and simplicity.

I know that banks have this ability but I realy want to try and avoid the overhead of the function calls.

Perhaps someone knows also if using this method will use the CPU cache efficiently, because I heard way back that BlitzBasic banks missed the cache.

Thanks

MemAlloc should do that, but you can just as well use an array.

If you plan to use it within BM, don't use MemAlloc but TBank and CreateBank.

@Dreamora
Using TBank, will that not give me overhead on the function calls?

@grable
Can I just use MemAlloc to get a pointer to a block of memory then do this:

local a:float = 0.4432 ' example values
local b:int = 1232
pointer[ mem_address1 ] = a
pointer[ mem_address2 ] = b

Will that poke both ints and floats into the memory? If so then that is excactly what im after.

Ok, I just coded up a small sample:

Global pointer:Byte Ptr
pointer = MemAlloc( 255 )
MemClear( pointer, 255 )
pointer[0] = Int( 123 )
pointer[2] = Float( 1.45 )

This compiles and appears to be working. The problem is that as the float is placed into the memory location it is cast to a byte and therefore loses all precision.

Is there anyway around this. I realy dont want to resort to fixed point numbers for speed reasons.

Perhaps im just indexing the memory wrong for my read/write opperations.

Thanks

Global pointer:Byte Ptr
pointer = MemAlloc( 255 )
MemClear( pointer, 255 )
Global intpointer:Int Ptr = Int Ptr(pointer)
Global floatpointer:Float Ptr = Float Ptr(pointer)
intpointer[0] = Int( 123 )
floatpointer[1] = Float( 1.45 )

Cool, thanks very much.
I had a look at the TBank source and this is the same way it was done there.