WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Persistent Verses Global Variables


Question:

Although I haven't downloaded it yet, I was reading the "release notes" for 2004D and I was wondering what the difference between "Persistent" variables and "Global" variables is.

Anyone care to explain it?

Answer:

Basically a "Global" variables can be accessed from anywhere.

A "persistent" variable is defined and used in a single UDF. But unlike other UDF variables, it "stays around". Kinda of like a private global vaariable that no other UDF's can see.

For, or example, if a UDF wants to save away information that it needs, it can us a persistent variable. The next time the UDF is called, its persistnt variable is still there, with what ever value was left over from the previous call to it.

In C/C++, this would be equivalent to a static variable, which would have a value that remains non-volative across invocations of the function.

Normally, all local variables within a function are volatile in that the storage for them is allocated on the stack *after* the function has been entered. When the function exits, the stack gets "popped" to free up the memory for local variables used by the function and thus the contents of those local variiables is lost.

When a variable in C/C++ is declared as static, it's allocation is still on the stack, but it's done much lower on the stack, such as befor the "main()", "winmain()" or "dllmain()" function is called and thus the storage for the variable is not deallocated until the program module is unloaded from memory. The net effect is that each time the function is called and the static variable is referred to, it's contents are valid assuming that it was initialized or otherwise written to at least one time to store a known valid value in it.

So, just consider a persistent variable in WIL to be the same as a static variable in C/C++.


Article ID:   W16978
File Created: 2007:07:03:14:27:26
Last Updated: 2007:07:03:14:27:26