I always prefer not to use PEEK/POKE but try to use Virtual Variables.
Below an example that I hope will clarify something more.
It is long because I added many comment to explain various steps. Please read comments as a continuous speech.
In general the message I would like to give is: memory is just a sequence of bytes regardless the way you access or interpret it !
Using Virtual Variables you can interpret memory the way you want in different ways all at the same time.
Uses "Dictionary"
'---Here you structure
Type t_Vec3d
X As Double
Y As Double
Z As Double
End Type
'Dim Positions() As t_Vec3d
Dim MaxPositions As Long = 123
'ReDim Positions(nPositions)
'---Here we define a dictionary able to create 1000 buckets
'---1000 is not the max number of keys the dictionary will be able to store
'---but is the number of pre-allocated buckets for the internal hash table value calculation.
'---If different keys will produce the same hash value, dictionary will create
'---a linked list of keys for the same bucket
Dim myData As DWord = Dictionary_Create(1000)
'---We want to use this dictionary to store all main application info
'---So we store some of them
Dictionary_Add(MyData, "APP_NAME" , "Test dictionary power")
Dictionary_Add(MyData, "APP_AUTHOR" , "Rene Miner")
Dictionary_Add(MyData, "APP_COAUTHOR" , "Eros Olmi")
'---Ok, after few APP info, some real data
'---To store an array of t_Vec3d we just need to allocate the correct memory for it
'---If we know the definitive number of elements, it is quite simple to
'---calculate the needed memory: SizeOf(t_Vec3d) * MaxPositions
'---So we create a dictionary element (key + memory) for this
'---We will call it DATA_POS and will allocate enough memory full of nulls so
'---all memory will be initualized to nul
'---For later fast usage, we will also save a pointer to the first data byte
Dim pDataPos As DWord
pDataPos = Dictionary_Add(MyData, "DATA_POS" , String$(SizeOf(t_Vec3d) * MaxPositions, $NUL))
'---Dictionary_Exists can be also used to get back pDataPos
'pDataPos = Dictionary_Exists(MyData, "DATA_POS")
If pDataPos Then
'---All is OK
Else
'---Something went wrong, better to give error
End If
'---Here we assume all went OK
'----------------------------------------------------------
'---Now how to access single t_Vec3d element in the array?
'----------------------------------------------------------
'---Imagine we want to access t_Vec3d(100) element
Dim ThePosWeWant As Long = 100
'---1. easy way: create a DUMMY VIRTUAL t_Vec3d array variable
'--- pointing its memory to the correct memory position
Dim lVec3d(MaxPositions) As t_Vec3d At pDataPos
lVec3d(ThePosWeWant).x = 1
lVec3d(ThePosWeWant).y = 2
lVec3d(ThePosWeWant).z = 3
'---DIM ... AT will never allocate or de-allocate any memory but will only
'---create a dummy variable that uses the memory allocated by others
'---2. little less easy way: create a DUMMY VIRTUAL t_Vec3d single variable
'--- pointing its memory to the correct memory position making some element calculation
Dim lVec3dElem As t_Vec3d At pDataPos + (SizeOf(t_Vec3d) * (ThePosWeWant - 1))
lVec3dElem.x = 4
lVec3dElem.y = 5
lVec3dElem.z = 6
'---You can always set the memory to which a dummy variable point using SetAt
'---so if you want to access position 10 ...
ThePosWeWant = 10
SetAt(lVec3dElem, pDataPos + (SizeOf(t_Vec3d) * (ThePosWeWant - 1)) )
'---Now we have pointed lVec3dElem at position 10
lVec3dElem.x = 10
lVec3dElem.y = 20
lVec3dElem.z = 30
Bookmarks