Link to home
Start Free TrialLog in
Avatar of keyboards
keyboards

asked on

how do you dereference a variable pointer and get it's value back?

Seen many articles here on the use of varptr() to get the address of a variable, but how do you dereference it to get a variable's value from the address?
Avatar of JR2003
JR2003

What type of variable are you getting the address of?
You can use rtlMoveMemory API (aka CopyMemory) to dereference a pointer:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()

    Dim lSource As Long
    Dim lDest As Long
    Dim lPointer As Long
   
    lSource = 101
    lPointer = VarPtr(lSource)
   
    'get the value from the pointer
    Call CopyMemory(lDest, ByVal lPointer, LenB(lSource))
   
    Debug.Print lDest

End Sub
Avatar of keyboards

ASKER

So, at least as far as the above technique, you can't dereference the address to see what's there directly, but have to copy what's there to another location that is already of the correct type, yes?

Truthfully, I have dynamic arrays of user defined types, and I'd just like to be able to pass any of these arrays to a function.  But the arrays are not pre-declared, and have to be allocated at run-time.

Is there such a thing as a collection of arrays?  Or any other VB object type that would allow dynamic instantiation of at least a reference to something dynamically dimentioned?

I appreciate any advice, unless it's going to be "do it in C" *lol*; which I know would be a snap after porting.  Just can't screw myself up to the porting effort without exhausting all VB options. ;0}

Regards
As long as you know the Type of the array, you can pass a dynamic array of user Types to a function.

e.g.

Private Sub Command1_Click()

    Dim udtArray() As MyType
    Dim i As Long
   
    For i = 0 To 9
        ReDim Preserve udtArray(i)
        udtArray(i).member1 = i
        udtArray(i).member2 = i + 100
        udtArray(i).member3 = i + 1000
    Next
   
    Call PrintUDTArray(udtArray())
   
End Sub


Private Sub PrintUDTArray(udta() As MyType)
   
    Dim i As Long
   
    For i = 0 To UBound(udta)
        Debug.Print udta(i).member3
    Next
   
   
End Sub
you didn't pass it the address of the array.  I don't know what the name of the array will be at "compile time", it's dynamically allocated at run time.

I specifically need to know the syntax for obtaining, passing, and dereferencing the address of the array, without knowing it's name.
...and I guess I have to add "without copying the entire contents of the array passed to the function into another variable within the function". The dynamic arrays hold structures representing event absolute time, duration and magnatude and there might be a few million in a given array; this makes repeated copying prohibitive....unless we know the entire size of the array in memory and it's guarenteed contiguous and can be copied as a chunk relatively quickly and THEN dereferenced within the function.
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also be advised that CopyMemory does no bounds checking.  If you attempt to read or write to memory outside the heap, the program will crash and burn.  You must know that the pointer is valid and the extent of the array.

Save your work before running in the IDE.
Great stuff Erik, thanks for all the advice and code.  It shows me what the capabilities and limitations of VB are and that's it'll be quite a poor choice for this project, though it's done well enough for the slightly dumbed down draft version.

Imagine a mess of real-time events as input, each of a different, uncombinable type.  But you don't know what types there can possibly be or what event types you're going to get on any particular run, nor how many unique event types.  Similar types of events must be delt with along side each other but separate from other types, so you have to allocate an array for each type as it comes in, populate it,  and be able to find it again when another event of the same type is encountered from the input.  Thus I have to create a logical index of keys and addresses for dynamically allocated arrays for each event type I encounter from the input.  It may just seem like sorting from this superficial description, or that some presorting/prefiltering should have been done to the input; I'll just have to assure you it cannot.  And all event types cannot be in one array with an event_type property for reasons I cannot mention here.  And there is too much data to physically copy each time a new event type is encountered when there are potentially millions of events of each of the various kinds encountered at random distributions, and still meet performance requirements.

Lovely eh?  ;0}

Thanks again,
Don
Got this link http://www.ftponline.com/archives/premier/mgznarch/vbpj/2000/08aug00/bb0008/bb0008.asp from another VB solution post here on EE.  Excellent article on casting variables through function aliasing.  That's the effect I was looking for, though the technique is somewhat surprising.

Regards,
Don (aka Keyboards)