Re: Doubts on Defining and declaring variables
- From: "Anthony Jones" <Ant@xxxxxxxxxxxxxxxx>
- Date: Wed, 27 Sep 2006 22:31:41 +0100
"divkavya" <divya_sanam@xxxxxxxxx> wrote in message
news:1159354154.785768.23910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Kindly Correct the places where my understanding is wrong.
What happens When we define or declare a variable?
1.Dim iage
My understanding :-For this line No spaceis reserved nor
memory allocation done at this point compiler only knows that
this variable may be used in the program and is variant)
VBScript will look ahead to find all Dim variables in the current execution
and allocate memory for each of them
2.Dim iage As Integer
My understanding :-Still No memory allocation done at this
time ,compiler only knows that this variable may be used in the
program and is Integer so gives error if something else is put .This
statment can be given only when Option Strict is on.)
Me thinks you may be confusing VBScript with VB.NET only VB.NET
implements Option Strict in VBScript you can not give types to variables.
3.Dim iage
iage = 5
My understanding:-Here memory allocation
is done and suppose at location 1000.since 5 is being
assigned so late binding is done wherein it treats iage as integer and
assigns 2 bytes of memory)
VBScript will already have allocated the 16 byte variant structure for iage.
iage = 5 simply stores the integer value 5 in the structure and sets it's VT
Type
to indicate that it current is storing an integer.
iage="String"
Now since string is assigned to iage ,So will
now the "string" be written from location 1000 ,or at a
new location??)
The "String" literal will be in p-code built by the VBScript parser. Then
the line
is reached where it is assigned to iage a new string is allocated from
memory and
"String" is copied to this memory. A pointer to this newly allocated string
is then
stored in the variant structure represented by iage and it's VT type set to
indicate the
variants holds a string.
6.
dim arr(1)
What happens in case of arrays when I give dim arr(6)?? Is the space
reserved in side memory for storing 2 elements?? My understanding :-No
space reserved at this point
Dim arr(1)
Will cause an array containing 2 elements to be allocated immeadiatly.
arr(0) = 45
arr(1)= 50
My understanding :- If arr(0) is stored at location 1000 since integer
so 2 bytes so 1000 and 1001 will together store 45 and now arr(1) will
be stored at 1002 and 1003.
Each element in the array is 16 Byte variant.
5.
How is the memory allocation done when we use Redim?
I mean if we hav a array suppose
Dim arrNames(1)
arr(0)= 5
arr(1)= 6
...
...(other lines of code)
...
Dim fig
fig= 7
...
...
ReDim Preserve arrNames(2)
arrNames(2)=8
Now in this case how is memory allocation done, arrNames(0) and
arrNames(1) will be continous and where is arrNames(2)element stored ??
I mean what happens when u give:-
ReDim Preserve arrNames(2)
arrNames(2)=8
Most likely the new memory is allocated large enough to hold the new size of
the array
The original contents is copied to the new array and the original array is
deallocated.
It may be that an internal optimisation may spot the enough adjacent memory
is
available to extend the array but it's doubtful.
Does it add the arrnames(0),arrnames(1),arrnames(2) into a new memory
location or just does memory allocation for arrnames(2) and link it ??
Memory for an array has to be contiguous.
I mean suppose befor redim arr(0) was at locations 1000 and arr(1) at
location 1002 .
Now after redim what happens ?
Does it copy values arr(0),arr(1) and arr(2) into new location,suppose
2100 for arr(0) and 2102 for arr(1) and 2104 for arr(2)
or
it allocates memory for only arr(2) at 2100 and somehow links it to
location 1002 ?
Nope see above.
6. How are strings stored in the memory? Is some thing like "\0" stored
the end of the string like in C???
Strings in VBScript are BSTRs. These are unicode (2-bytes per character).
They are terminated by a null but this is for compatability with API C based
functions.
A BSTR string has a 4-byte prefix which specifies the length of the string.
Kindly clear my doubts and please correct the place where my
understanding is wrong. Kindly suggest a book or link which explains
the memory allocations in VBScript.
TBH, If this is really a concern for you then you'd be better off not using
VBScript.
Regards
Divya
.
- References:
- Doubts on Defining and declaring variables
- From: divkavya
- Doubts on Defining and declaring variables
- Prev by Date: Re: Loading variables from text file
- Next by Date: Re: Script doesn't work
- Previous by thread: Re: Doubts on Defining and declaring variables
- Next by thread: Script to create groups
- Index(es):
Relevant Pages
|