Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shifting objects in memory (this ain't in the FAQ! ) 1

Status
Not open for further replies.

Bobofbobland

Programmer
Mar 28, 2003
8
GB
HI,

I've coded myself into a bit of a corner here. I'm manipulating silly amounts of data in VB. Enough that I wanted to avoid Collections and Dictionaries, and instead have created wrapper classes around arrays.

Also, I should mention at this point they are arrays of objects.

Now I have the age old problem of removing something from the middle of the array. I could create a queue system as the indexing of the array is not too important (for once), but I'd prefer not to add that complexity. Ideally I'ld like to just shift the whole area of memory down a bit. Can you tell I'm a C programmer?

I've tracked down the RtlMoveMemeory API function (CopyMemory) which I know I can use for Longs... but can I use it for my class instances. And does it make a difference if I'm using Obj or MyClass as the type. I'm told the class reference is always 4 bytes long. So everything is okay, yes?

Any confirmation would be helpful. I'm open to other suggestions as to how to accomplish all this though, hence the interminable rambling at the start...

 
Some sample code from your program will help us better understand your problem.
 
Here you go... something like this:

Dim MyArray[3000] As MyClass
Dim ThingsInList As Long

Function RemoveObject(Index As Integer) As Boolean

If Index < ThingsInList Then
CopyMemory MyArray(Index), MyArray(Index + 1), _
(UBound(MyArray) - Index) * LenB(MyArray(Index))
RemoveObject = True
Else
RemoveObject = False
End If

End Function
 
Yes your code is alright.
I think you are using the variable ThingsInList to maintain the number of valid entries in the array.

After CopyMemory, you may also resize the array so that the array shrinks automatically when the element are removed and there are no duplicate elements in the end of the array.

Redim Preserve MyArray(LBound(MyArray) To UBound(MyArray) - 1)

Moreover, you may also replace the element &quot;LenB(MyArray(Index))&quot; by 4 for simplicity as it will always evaluate to 4.
 
Bob -
You know you can do linked lists in VB?

You define a class to hold your data, plus a &quot;next&quot; pointer of your class's type. Or two, if you're doing a doubly-linked list.

When you want to remove an item from the middle of the list, you just rearrange the object pointers as usual.

Because you're using small objects and connecting them together, needing large amounts of contiguous memory isn't required as it would be using arrays.

Chip H.
 
Thanks to both of you. A linked list would have been an equally elegant way of doing things, but I guess I miss real pointers and my days writing device drivers ;)

CHeers,

BoBL.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top