When you think ASP, think...
 Recent Articles All Articles ASP.NET Articles ASPFAQs.com Message Board Related Web Technologies User Tips! Coding Tips

 Sections: Sample Chapters Commonly Asked Message Board Questions JavaScript Tutorials MSDN Communities Hub Official Docs Security Stump the SQL Guru! XML Info Information: Feedback Author an Article

 Jump to a FAQ Enter FAQ #: ..or see our 10 Most Viewed FAQs.

# 4GuysFromRolla.com : ASP FAQS : Arrays

Question:

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

[Print this FAQ]

 `This message is a replica of Bill Wilkinson's post to the ASP Messageboard on March 23rd, 2001[http://www.aspmessageboard.com/forum/Advanced.asp?M=182467&P=1&F=27]`

The memory for a two-dimensional array is laid out the same as single dimensional arrays. Except that the "rows" are themselves contiguous. (If you've not already read the FAQ "How is an array stored in memory? I am interested in learning about the internals of an array!" be sure to do so now before continuing on with this FAQ.)

To illustrate, let's say you do:

 `Dim ar(1,3) `

Then 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) `
So 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 `

See it? `ar(1,2) -->> 2 * ( UBound(ar,1)+1 ) + 1 -->> 2 * 2 + 1 -->>` element number 5 [starting at 0, of course].

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

So, 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.

Now...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)`. What does the language have to do?

First, it allocates enough memory for the new array: `16 * 2 * 6` bytes (128 bytes). Then it simply copies the old array, starting at its starting address and FOR THE SIZE OF THE OLD ARRAY to the starting address of the new array. Presto! It is done! (well, the elements of the new array beyond the end of the old have to get zeroed out, but that's a detail.) If it did the memory layout any other way, ReDim Preserve would be a *LOT* more work! And note that, because of the stuff discussed above, none of the elements in the newly sized array need to be touched, at all! Primitive values (numbers, etc.) are of course still fine. And pointers to objects (including strings) are still...well...pointing to the right place!

[ I leave it as an exercise for the reader to figure out what the system does if the ReDim size is smaller than the old size. ]

FAQ posted by Bill Wilkinson at 3/23/2001 7:26:46 PM to the Arrays category. This FAQ has been viewed 53,696 times.

 Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions! Most Viewed FAQs: 1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views) 2.) I am using Access and getting a 80004005 error (or a `[Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)'` error) when trying to open a connection! How can I fix this problem? (207777 views) 3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views) 4.) How can I quickly sort a VBScript array? (196039 views) 5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views) 6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views) 7.) When I get a list of all files in a directory via the `FileSystemObject`, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views) 8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views) 9.) Can I send emails without using CDONTS? (107083 views) 10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views) Last computed at 9/17/2007 3:22:00 AM

 ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article

 #developerBrand-footer{text-align:center;margin:0 auto} #footerlinks{font-size:12px;word-spacing:1px} #footerlinks a{color:#000;margin:5px 0 0} #itbeBrand-footer{text-align:center;margin:0 auto} #itbusinessedge-footer{text-align:center;margin:0 auto} #footercopyright{font-size:12px;margin:5px 0 0} #developerBrandForum-footer{text-align:center;margin:0 auto} #itbeBrandForum-footer{text-align:center;margin:0 auto} #eweek-truste-footer{text-align:center;margin:0 auto} #developerBrand-truste-footer{text-align:center;margin:0 auto} #itbeBrand-truste-footer{text-align:center;margin:0 auto} #weboitbeBrand-truste-footer{text-align:center;margin:0 auto} #itbusinessedge-truste-footer{text-align:center;margin:0 auto} .footerLogoClass{text-align:center;margin:0 auto}