post your comment   print   send to a friend
Rate:   0% | Views: 515
Question categories:  ASP

How is a two-dimensional array laid out in memory?

The memory for a two-dimensional array is laid out the same way as a single dimensional array, except that the "rows" are contiguous. For example, if you write

Dim ar(1,3)

in memory you would find all the elements, contiguously, in this order:

ar(0,0) -- ar(1,0) -- ar(0,1) -- ar(1,1) -- ar(0,2) -- ar(1,2) -- ar(0,3) -- ar(1,3).

To find element ar(x,y), you can treat this as a single dimensioned array and find the element numbered as:

y * ( UBOUND(ar,1) + 1 ) + x

That is, ar(1,2) -->> 2 * ( UBound(ar,1)+1 ) + 1 -->> 2 * 2 + 1 -->> element number 5 [starting at 0, of course].

If you count to element number 5 in the above code, you will see that it is, indeed, ar(1,2).

Thus, to get the address of that element, you simply multiply that result by 16 and add it to the address of the start of the array.

Why is this scheme chosen by VBScript? Simply so that ReDim Preserve is easier to implement.

Suppose you start with that array above, and then do ReDim Preserve ar(1,5). First, enough memory for the new array: 16 * 2 * 6 bytes (128 bytes) is allocated. Then it simply copies the old array (beginning at its starting address) to the starting address of the new array. (NOTE: The elements of the new array beyond the end of the old need to be zeroed out.) If it created the memory layout any other way, ReDim Preserve would be a lot more work. Note that none of the elements in the newly sized array need be touched. Pointers to objects (including strings) are still pointing to the right place. Primitive values (numbers, etc.), of course, can still be used.

Customer Feedback
Rate:   0% | Views: 515 | Please Rate:  
 
If you have other comments or ideas for future technical tips, please type them here:

Email: (optional)

Comments: (optional)

 Asp Hosting | Web Site Design    Back to serch results
Browse the Base
Knowledge Base
Web Design
  Do It Yourself
    ASP
Messages
 

$75 Free Google AdWords

Free $75 Google AdWords when you sign up for WebImage! Target by location, create your own, or let Google create your ads for you. Check out http://www.aplus.net/google.html to see how AdWords works for you.

Private Area
 
Ask
in Private
   
Personal
Folder
 
Related Questions
 
1. How can I display all of the contents of a single-dimension array?
 
2. Does ASP.Net still recognize the global.asa file?
 
3. Is it possible to run client-side .NET code within a browser?
 
4. What happened to date() and time()?
 
5. How do I display data on a web page using arrays instead of Do...While...MoveNext...???
 
Home Browse Search Ask in Private Personal Folder   Help
powered by web hosting 
  Logged as: Guest