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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy array elements using CopyMemory

Status
Not open for further replies.

ESquared

Programmer
Dec 23, 2003
6,129
US
I'm transposing a 2-dimensional Variant array (subtype String) and would like to speed it up using CopyMemory to copy string pointers instead of actually making copies of all the strings. (Of course, if you have a better suggestion, I'm quite open to other ways to accomplish this task.)

So first, is this feasible or will I be causing memory problems unless I actually swap the string pointers?

And second, I haven't yet made the operation work successfully, probably because I just am not practiced enough with pointer manipulation and VB arrays.

Here's my original code. I've already examined the bounds of the input array and sized a dynamic array to the appropriate, transposed size. Note that the input array is coming straight from the GetRows method of an ADODB.Recordset.

Code:
For iColumn = iLBound1 To iUbound1
   For iRow = iLBound2 To iUbound2
      vasTransposed(iRow, iColumn) = vasValues(iColumn, iRow)
   Next
Next
So next step, I put in the function header for the movememory API:

Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        Dest As Any, _
        Source As Any, _
        ByVal bytes As Long _
    )
And I began with:

Code:
CopyMemory vasTransposed(iRow - HeaderRow, iColumn), vasValues(iColumn, iRow), 4
Now I know that VB strings are actually pointers to the string location, so I tried ByVal. I tried VarPtr. I tried StrPtr. I tried "ByVal Dest As Long" and same for Source. But no go (and a couple of crashes, of course).

Could someone help set me straight, not just to get working code but give me some pointers (no pun intended :) ) on how to approach this sort of problem in the future?
 
AFAIK, the result is not a Variant array of String at all. You get a Variant that contains a 2-D array of Variant.

So you have a little indirection just to start with, and even the inner array is not of String at all. I suspect ADO coerces each value from the data source type to the closest matching Variant subtype.

I doubt CopyMemory is going to be much help even if you unravel things. Even with an actual array of String I doubt using it for transposition would be much faster than simply using String assignment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top