?
Solved

Retrieving an array's pointer

Posted on 2003-02-23
10
Medium Priority
?
249 Views
Last Modified: 2010-05-01
Before the question, I need to be sure we're straight on a few facts:

VISUAL BASIC & ARRAY POINTERS

Dim MyArray(2) As String
MyArray(0) = "jim"
MyArray(1) = "joe"
MyArray(2) = "jon"

In memory the array is comprised of three 4 byte elements.  Each element is a pointer to the actual string.  To get the element's address in memory you use VarPtr(MyArray(X)).  The easy way to get that element's value you may use StrPtr(MyArray(X)).

So here we have an array floating in memory:

Address: VarPtr0 VarPtr1 VarPtr2
Value: StrPtr0 StrPtr1 StrPtr2

It's an easy thing to replace the values for each element so that each element points to a different string.  You can find numerous articles on string sorting algorithms using such a technique.  However, I need the array variable itself to point to a completely different array.  Now of course, we could do something like this:

'We create an array with a null pointer
Dim Array1() As String
'We create an array with a pointer to an allocated array
'(although, each element is a null pointer)
Dim Array2(2) As String

Array1 = Array2

Array1 now has 3 elements.  Seems like we did a pointer assignment.  But we didn't.  Cause if you do this test:

Debug.Print VarPtr(Array1(0)), VarPtr(Array2(0))

You'll find that the element addresses are completely different.  If we DID assign a pointer to the entire array to Array1, then if you modified Array2, you'll be modifying Array1.  But that is not the case.  Instead, VB copied the array.

THE SITUATION

I have a dynamic 3 dimensional array.  I am not allowed to ReDimension the array's first 2 dimensions.  However, it is required that this array be completely dynamic, and I've been searching for workarounds.  My favorite idea is to create a new array by the required dimensions and swap the 2 array variable's pointers.  That way when the routine ends, VB will free up the memory pointed to by the temporary array variable, and leave my original array variable, and its new contents, alone.

Nice story huh?  Well, I know the theory works cause it works like that with individual elements.  You can swap element pointers and have a happy day.

THE PROBLEM

I can't get the address of an array variable.

Diagram:

MyArray---->ADDRESS==ptr1-->VALUE==ptr2
MyArray(0)->ADDRESS==ptr2-->VALUE==strptr

The variable MyArray points to the first element of the actual allocated array.  The VALUE of MyArray isn't hard to get.  All you have to do is VarPtr() the first element.  But I don't have the ADDRESS of MyArray (ptr1 in the diagram).  I need that address in order to change the value to the pointer of a different array.  VarPtr(MyArray) does not work.  I get type errors.

*whew* that's it.  Sorry for being so verbose,  but I wanted to be absolutely clear.
0
Comment
Question by:SilentRage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8003369
Would this wrk as a work around

Private Function GetPointer(obj As Variant) As Long
   GetPointer = VarPtr(obj)
End Function
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8003386
Forget that
0
 
LVL 2

Expert Comment

by:muskad202
ID: 8004040
what about objptr ??
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:SilentRage
ID: 8004465
what about objptr.  That's for retrieving the pointer to an object variable.  That doesn't apply here.
0
 
LVL 1

Author Comment

by:SilentRage
ID: 8009671
I found a suitable solution.  Here's the function that will allow you to have complete dynamic control over resizing a multi-dimensional array.

I didn't bother to make this function generic.  So feel free to modify to your personal needs.  In this example, you should know that 'Maze' is a 3 dimensional array.  Also, after resizing it, it has all original data (Preserved).  Also, 'MazeRoom' is a user-defined type.

Public Function Resize(ByVal X As Integer, ByVal Y As Integer, ByVal Z As Integer)
Dim TmpMaze() As MazeRoom, IUB As Integer, JUB As Integer, KUB As Integer
    'Copy the original array to a temporary array var
    TmpMaze = Maze
    'Erase the original array
    Erase Maze
    'Redimension the original array to the new larger or smaller size
    ReDim Maze(X, Y, Z) As MazeRoom

    'Calculate the upper bound to be used when coping data back in
    IUB = IIf(UBound(TmpMaze, 1) > X, UBound(TmpMaze, 1), X)
    JUB = IIf(UBound(TmpMaze, 2) > Y, UBound(TmpMaze, 2), Y)
    KUB = IIf(UBound(TmpMaze, 3) > Z, UBound(TmpMaze, 3), Z)

    'Now we 'Preserve' the data by copying it back to the newly dimensioned array
    For I = 0 To IUB
        For J = 0 To JUB
            For K = 0 To KUB
                'Do a pointer/value copy (will not work for Integer or byte arrays)
                CopyMemory Maze(I, J, K), TmpMaze(I, J, K), 4
            Next
        Next
    Next
End Function

I may have found the solution to my programming problem.  But if there's any way to get the array pointer, that would make things a lot easier - and efficient.

also: general question.  If nobody can give me an answer - or if there isn't an answer - is there any way to reclaim my question points?
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8009818
Goto Community support -> Cleanup -> Ask a question

Question title Please delete Question

Question Body Link to question
0
 
LVL 1

Author Comment

by:SilentRage
ID: 8009986
thanks.

Small bug.  Serves me right to post before testing.  Change out the > with < in the upper bound calculation.  Also, since I'm making another post anyway.  That part where I say that it doesn't work for byte or integer arrays?  that line of code has a 4 on the end.  Change to 1 for byte arrays.  Change to 2 for integer arrays.
0
 
LVL 1

Author Comment

by:SilentRage
ID: 8010661
thanks.

Small bug.  Serves me right to post before testing.  Change out the > with < in the upper bound calculation.  Also, since I'm making another post anyway.  That part where I say that it doesn't work for byte or integer arrays?  that line of code has a 4 on the end.  Change to 1 for byte arrays.  Change to 2 for integer arrays.
0
 
LVL 1

Author Comment

by:SilentRage
ID: 8010664
darnit.  don't ever refresh a page after posting...
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8104870
PAQ'd and points refunded.  

BTW, you can use the "reload this question" link at the top left instead of refresh.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question