Having two variable share the same space (memory address)
I remember attempting this a while ago with no joy. I want to do like one can in other languages by having two variable point to the same memory address.
I want to be able to overlay a string with a UDT or other structure or variable.
Has anyone been able to accomplish this?
Mainly a string overlayed by a byte array.
Re: Having two variable share the same space (memory address)
Well, as you know VB doesn't have pointers. But then again when you pass a variable ByRef to a procedure they do point to the same memory space... But they have to be of the same kind to do so.
There's probably a way you can work around this, but I need to know the purpose to be able to figure something out.
Re: Having two variable share the same space (memory address)
I wanted to overlay a string with a byte array so that I could reference the string in two different ways without coping it.
Re: Having two variable share the same space (memory address)
Code:
Option Explicit
Private Type SafeArrayBound
cElements As Long 'Length of array
lLbound As Long 'Low bound of array
End Type
Private Type SafeArray1D
cDims As Integer 'Number of dimensions
fFeatures As Integer 'Array construction flags
cbElements As Long 'Size of data elements; 1 byte, 2 integer or boolean, 4 long...
cLocks As Long 'Number of current locks (or current users)
pvData As Long 'Data address
rgSABound As SafeArrayBound 'see above
End Type
Private Declare Function VarPtrArray Lib "MSVBVM60.dll" Alias "VarPtr" (Var() As Any) As Long
Private Declare Sub PutMem4 Lib "MSVBVM60.dll" (ByRef inDst As Any, ByVal inSrc As Long)
Public Sub ShareStringMemory(sText As String)
Dim ArrayDesc As SafeArray1D
Dim TmpArray() As Byte
Dim ArrayPtr As Long
Dim i As Long
ArrayPtr = VarPtrArray(TmpArray)
With ArrayDesc
.cDims = 1
.cbElements = 1
.pvData = StrPtr(sText) '-4 for full BSTR
.rgSABound.lLbound = 0
.rgSABound.cElements = LenB(sText) '+6 for full BSTR
End With
PutMem4 ByVal ArrayPtr, VarPtr(ArrayDesc)
For i = 0 To UBound(TmpArray())
Debug.Print TmpArray(i);
Next i
PutMem4 ByVal ArrayPtr, 0& 'I think this is needed for some OS's
End Sub
Re: Having two variable share the same space (memory address)
If I'm not mistaken, when the garbage collector picks up one reference then the other reference will run into memory related errors.
And the BSTR address isn't of much help if your gonna do string manipulation
Re: Having two variable share the same space (memory address)
Quote:
Originally Posted by leinad31
If I'm not mistaken, when the garbage collector picks up one reference then the other reference will run into memory related errors.
...only the case with some OS's but thats why I copy a null pointer before it goes out of scope.
Quote:
Originally Posted by leinad31
And the BSTR address isn't of much help if your gonna do string manipulation
It is sometimes, but the above as it is just references the String's characters and not the full structure. I find it useful when working with very large strings where the native string functions don't cover what I want to do, or are just too slow.
Edit: Having said that, this sort of thing is GPF heaven and should be coded very carefully.
Re: Having two variable share the same space (memory address)
Well if your gonna use the reference immediately (or always updated just before processing) then problems should be minimal...
Re: Having two variable share the same space (memory address)
Milk,
That is very interesting, I would take it that all the strings are stored in unicode format and you have to take that into consideration. Is there anyway to check with to make sure.
I am wondering (an I will need to test this) that if the string is read from a file then there will be no unicode format since it should be a binary data string. So, I would need to check to make sure of the storage format. Is there a way?
Re: Having two variable share the same space (memory address)
Ok, it seems that even with a string read from a file it is still stored in unicode format. So directly addressing the byte array with your code would need some conversions.
But I like what you have done...
Re: Having two variable share the same space (memory address)
Tested code using a byte array as input and it works nicely, directly addressing each byte in the array. I even included an offset so that I can address inside the passed byte array.
NICE!!!!!
Thanks Much.
I will see what other niceties I can come up with.