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

How is an array stored in memory?

When you allocate an array -- say via Dim ar(500) -- the system allocates CONTIGUOUS memory sufficient to hold the requisite number of items (since VBS always starts arrays at zero, this means space for 501 items is allocated). All items are always the same size

When you ask for a particular element in the array [via x=ar(76)], the system simply multiplies the index [76 in the example] by the SIZE of one item, adds that number to the address of the start of the array, and presto! It is addressing the proper spot in memory.

How large is each item in the array?

In a language such asv/C/Java (or even standard VB) the size of each item depends upon the *type* of the array elements. For example, if you ask for an array of 100 ints in /C/Java, you will allocate 100 times 4 bytes, because an int is 4 bytes. If you allocate an array of 1000 doubles, you will allocate 1000 times 8 bytes.

VBScript and JScript are "typeless" languages. Any element of an array can hold *any* data value. If this is true, how can the system know in advance how big each element should be?

Every value in VBS and JS is an OLE VARIANT type. In /C/Pascal, it is a "discriminated union." That is, the first two bytes contain a number (a short integer) that tells *how* to interpret the rest of the bytes. The remainder of the bytes can be a Boolean, byte, int [see CInt, only 16 bits], long [32 bit integer], float, double, or a “pointer” to some more complex structure. (This includes pointers to STRINGS and pointers to objects.)

Every VARIANT uses 16 bytes of memory. Thus, every variable uses 16 bytes of memory. Therefore, every element of an array uses 16 bytes of memory! So, Dim ar(500) allocates 501 * 16 or 8016 bytes of memory.

If the type of the data is not one of the "primitive" (numeric, etc.) types, then the VARIANT holds a “pointer” to the actual data. So, when you do something like:

<%
ar = Array("this","is","a","demonstration")
Response.Write ar(3)
%>

This is what happens:

The system interprets the array as size 4 (elements 0 through 3) and allocates 4 times 16 bytes, then points the variable ar to those 64 bytes.

Then, it allocates space for a string containing this and puts the address of that string into slot 0 of the array. It then sets the first two bytes of that slot to indicate that the VARIANT points to a string.

Once this is done, it allocates space for a string containing "is" and puts the address of that string into slot 1 of the array. It will then set the first two bytes of that slot to indicate that the VARIANT points to a string.

Then it allocates space for a string containing "a" and puts the address of that string into slot 2 of the array. It then sets the first two bytes of that slot to indicate that the VARIANT points to a string.

When this is done, it allocates space for a string containing "demonstration" and puts the address of that string into slot 3 of the array. It then sets the first two bytes of that slot to indicate that the VARIANT points to a string.

Then, in the next line, when it sees ar(3) and picks up the address of the array. It sees the "3" as the element number, so it multiplies 3 times 16. It thus adds 48 to the array address and picks up the 16 bytes of memory that appear at the calculated address. It looks at the VARIANT it thus acquired and sees that this is a STRING, so it extracts the StringAddress variant from the known spot in the VARIANT. It now understands that this is the address of some STRING, and will now Response.Write the string.

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

Email: (optional)

Comments: (optional)

 Web Site Design | Hosting Service    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...???
 
Related Articles
 

Dedicated Web Server Checklist.

Own website hosting - risks and advantages.

Home Browse Search Ask in Private Personal Folder   Help
powered by web hosting 
  Logged as: Guest