Re: Doubts on Defining and declaring variables

Tech-Archive recommends: Speed Up your PC by fixing your registry



I don't think most of your questions are
really very relevant in VBScript. VBS is
extremely inefficient and you have virtually
no control over memory use, so there isn't
much point in thinking about it at that level
of detail.
(Looking at a typical script you can see
that people have to do things like load an
entire IE instance just to show a file browsing
window. That's probably thousands - maybe millions -
of times more wasteful than an unused variable.)

In VB, dimming initializes the variable. In
most cases that's 1-4 bytes. (2 bytes for
integer, 4 bytes for long integer, 4 bytes for
32-bit string pointer, etc.)
In VBS there are only variants and object
pointers. A variant is 16 bytes, packaging
the data type information inside that 16
bytes. So in VBS you've already lost direct
access to the data/memory when you declare
the variable.

VBS is designed to be simple and quick to
write. To accomplish that it hides most of the
work. You don't even have to dim variables if
you don't want to, and you usually don't have to
be concerned with releasing objects. It's like
a 4-year-old eating lunch. All you have to do is
eat. Someone else will clean up. (Hopefully. :)

There's no such thing as: Dim r = 8
That's only in javascript.

Concerns about the compiler are also not very
relevant with VBS. It's not compiled. It's interpreted
by wscript or cscript. The whole thing is no more
than a literal-style automation system. You tell
wscript what you want, using loose variant datatypes
and flexible language. Then wscript sorts it out, handles
all the details, and carries out what you're asking for.

If you want it simple, flexible and quick to write you
use script. If you want it lean, fast and competent (in
the sense of having a wide range of abilities) you
use a compiled language.

....And a third option, if you own a lot of stock in a
RAM-manufacturing company, would be to use .Net.
See this interesting article by Mark Russinovich:
http://www.sysinternals.com/blog/2005/04/coming-net-world-im-scared.html

He notes that a .Net version of Notepad requires
8 times the memory of the normal Notepad just to
sit there. That provides some perspective on the
issue of memory use, to my mind. Which is not to say
that it's not relevant, but that when you get into things
like script and .Net, convenience and ease-of-use are
the major factors, while efficiency depends on having
a fast CPU with lots of RAM. ... And that's not to mention
the phenomenal bloat of XP and the unspeakable
bloat of Vista. If you're dealing with any of that stuff
then concerns with memory usage by variables seems
"quaint".

--------------------------------------------------------------

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 space
is reserved nor
memory allocation done at this point
,compiler only knows that
this variable may be used in the
program and is variant)

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.)

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 isdone
wherein it treats iage as integer and
assigns 2 bytes of memory)

iage="String" (Now since string is assigned to iage ,So will
now the "string" be
written from location 1000 ,or at a
new location??)

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

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.


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

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 ??

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 ?

6. How are strings stored in the memory? Is some thing like "\0" stored
the end of the string like in C???


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.

Regards
Divya



.



Relevant Pages

  • Re: Internet Explorer and Memory
    ... any objects to be destroyed and memory to be recovered. ... after termination of the child vbs it's iexplorer remains in the ... >without corresponding call to IEStop()? ... >If not IsObjectThen Exit Sub ...
    (microsoft.public.scripting.vbscript)
  • Re: Internet Explorer and Memory
    ... any objects to be destroyed and memory to be recovered. ... after termination of the child vbs it's iexplorer remains in the ... >without corresponding call to IEStop()? ... >If not IsObjectThen Exit Sub ...
    (microsoft.public.scripting.wsh)
  • Re: VertexBuffer Memory Limit
    ... vbs certainly can store more than 64k of data. ... at a vertex format size of 32bytes, that seems to be>600m of vertex data. ... what OS, what main memory? ... what graphics card and what graphics card memory? ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: want to call DLL code from vbs
    ... there's also no direct access to variables in memory. ... Everything in VBS is a variant. ... Gary Roach wrote in message ...
    (microsoft.public.scripting.wsh)
  • Re: Very Slow reading excel data into an array (while opened in ne
    ... How can I read an excel file into memory without opening it? ... data array gets recycled each time it opens a new file so I don't have to ... Dim rowsMaster, colsMaster, lastCellMaster ...
    (microsoft.public.excel.programming)