Re: Doubts on Defining and declaring variables




"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



.



Relevant Pages

  • Re: Problem with memory allocation
    ... I changed the variable to char array and instead of assignment stmt, ... I believe this assignment statement is causing some memory allocation ... I remember another problem in the past, where this string was overwriting ... I would also like to understand the memory allocation stuff... ...
    (microsoft.public.vc.mfc)
  • Re: allocating memory.
    ... So what would be the ASM equivalent of new memory ... Memory allocation is primarily an OS responsibility, ... OSes are almost certainly responsible for memory ...
    (alt.lang.asm)
  • Re: Problem with memory allocation
    ... perhaps you're running into a reference counting problem and you really are running out of memory from simply allocating the string over and over. ... I believe this assignment statement is causing some memory allocation ... I would also like to understand the memory allocation stuff... ...
    (microsoft.public.vc.mfc)
  • Re: xmalloc string functions
    ... require memory allocations depending on the way the system works. ... Not enough context for most real-world applications to ... It is /more/ reliable to routinely auto-save the user's work (as you ... particularly if your auto-save code is robust against memory allocation ...
    (comp.lang.c)
  • Re: "Criticism of the C programming language ??????"
    ... whatever) is that GC only affects memory allocated by GC_malloc. ... other applications and third party libraries will use it too? ... I didn't see how that could be true since, like KT, I was thinking of garbage collection integrated into the language itself. ... In a later message, after a burst of silly and pointless sarcasm, you were persuaded to explain that you were talking about an additional memory allocation library which would support garbage collection for memory it controlled. ...
    (comp.lang.c)