Quote: "Is it like a variable?? Why nog use standard variables??"
Nah, they're more like arrays.
Array's are nothing more than a chunk of memory we can access (read from, write to) via it's name and a position index within that array.
eg,
Dim MyData(100)
MyData(50) = 123456
print MyData(50)
Sync
Wait key
Above we're created an Integer array called
MyData() and storing a single integer value inside it at position 50. It's not the only thing in the array though, all the positions (legal indexes) are initialized to zero.. So if we printed out the contents of index 49, it'd be zero at this point.
But what does it do ? Nothing really, arrays are nothing more than a user friendly interface that your programming language provides us with to interact with your computers memory. The programmer associates all meaning to what the data within any array represents.
eg,
Dim HighScores(10)
HighScores(1) = 10000
HighScores(2) = 9000
HighScores(3) = 7500
HighScores(4) = 6000
HighScores(5) = 4500
HighScores(6) = 3000
HighScores(7) = 1000
HighScores(8) = 700
HighScores(9) = 400
HighScores(10) =100
For lp=1 to 10
print HighScores(lp)
next
Sync
Wait key
In this example, we're using an array to store our games high score table. But really the computer doesn't have a clue what a high score is and why we're using this array in our program. To it, this is just another chunk of data. Internally the array is really just a lump of system memory. The language is just giving us a nice safe and organized way to access the information stored within it that chunk of memory.
Mem blocks are an alternative approach that we programmers can ask the language to give us a chunk of raw system memory. Computer memory is completely ambiguous. You can store anything you like in the chunk, from say integer, floats, string s through to picture data, sounds, text files through to machine code even. There's absolutely nothing special about it.
In DarkBASIC and DBPRO they've included mechanisms where the programmer can set out the data in the created mem Block that represents some internal media.
VanB's example above shows how you can manually build an image via the mem block system for example.
When we do this, we've peek and poking the raw image data into the chunk of memory in fashion that some built in conversion function understands. So you'll find commands that convert mem blocks to images and vice versa. Those commands look at the data in a membock and attempt to create a drawable image from it. They require the programmer set out the data in the mem block in particular way. Generally there's a header with a group of control variables followed by the raw data.
One fundamental difference between the Array() and memblock interfaces is that Integer Arrays house integer values, where mem blocks house raw bytes. An integer is made up of 4 bytes, so an array of 100 elements is actually 100*4 bytes (plus whatever header data they store in front of that). The programming language limits what types of data a programmer can be write/read from any array, based upon that arrays type. So a floating point array can only store floating point values in it for example. Where a mem block has no such limit.
To emulate the high scores table in a mem block, we get something like this..
rem alloc mem block #1 of size (10*4)
rem the size is 10*4 since we need 10 values
rem and each integer value is 4 bytes wide.
rem The memory in this block should be clear to zero
make Memblock 1, 10 * 4
rem fill the chunk of memory with out list of scores
write memblock dword 1, 0*4 , 10000
write memblock dword 1, 1*4 , 9000
write memblock dword 1, 2*4 , 7500
write memblock dword 1, 3*4 , 6000
write memblock dword 1, 4*4 , 4500
write memblock dword 1, 5*4 ,3000
write memblock dword 1, 6*4,1000
write memblock dword 1, 7*4,700
write memblock dword 1, 8*4,400
write memblock dword 1, 9*4,100
For lp=1 to 10
rem compute the offset of the score we want to access in this mem block
Offset = (lp-1)*4
rem read a dword (integer) from mem block 1 at this offset
print memblock dword(1,offset)
next
Sync
Wait key
rem Show the raw contents of the mem block in bytes
For lp=0 to get Memblock size(1)-1
rem read a each byte within the mem block and display it
print memblock byte(1,lp)
next
Sync
Wait key
The Memblocks commands are a high level approach to memory access. The language hides the physical location of the memory from you (you can get those if you really need) and gives you a safe way to read and write information into them. By safe, I mean that if you attempt to access memory outside of the chunks defined size, the language will give you a run time error. Such protections aren't possible with Pointer styled access. Which are at at lower level.
Related Articles:
*
What's a Memory Bank