Post Reply 
Variables global to a program
12-22-2013, 02:06 PM (This post was last modified: 12-22-2013 02:09 PM by Han.)
Post: #12
RE: Variables global to a program
(12-22-2013 05:51 AM)Michael de Estrada Wrote:  Wow ! So, if you fail to explicitly initialize variables that are used as counters you will get unpredictable results. Also, I created a second program TEST1() in a separate program group with MYVAR; and it started at 1 and is not in sync with the MYVAR in TEST(). So, the variable name is not unique, and values are being stored at different locations. This is crazy.

Also, if I change the variable name to say MYVAR1, it resets to zero and increments again. Just for yucks, I did a soft reset (On Symb), but it did not reset these values. I've never encountered any programming languages that worked like this, and am having a hard time understanding the purpose if any.

No, failing to initialize variables just leaves a default initial value of 0. If you were to open and close the source file, the count starts back at 0. When you created a new program group using MYVAR, it created a new variable MYVAR unique to TEST1(), unless you exported it. I presume that you did not, so that MYVAR for TEST() is different from MYVAR for TEST1(). Bascially, unexported global variables are unique even with the same name. Exported global variables, however, are not. If MYVAR were exported (in both programs) then as soon as you exit the source file for either, MYVAR gets reset to whatever the exiting source file declares. As I understand it:

1. All global variables retain their value if the program using them do not re-initialize their values. The initialization of a global variable outside of a program (i.e. in a variable declaration line written outside of a program's BEGIN/END block) happens ONLY during compile time (i.e. when you press Check or exit the editor). Note that source files are not re-compiled every time a program is run.

2. Non-exported global variables are unique to the source file that created them. That is, if you create two source files that declare MYVAR (unexported) then two separate memory locations are allocated, one for each source file.

3. Exported variables of the same name have the same memory location. So two source files that export MYVAR will create only one instance of MYVAR. The initial value of MYVAR will be whatever the most recently exited source declares.

4. #1. through #3. also apply to actual program names that define a BEGIN/END block.

Because exported objects share the same memory location, this is the only thing that could possibly cause ambiguity. That is, if one source file creates an exported routine called PROG1(), and you forget about it, and later create a new source file that also exports a different routine that happens to also be named PROG1(), then this new PROG1() can possibly break old programs that used the older PROG1(). You can temporarily fix it by recompiling the first source file (but then the programs in the newer source file may break).

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Variables global to a program - Bob Frazee - 12-22-2013, 01:09 AM
RE: Variables global to a program - Han - 12-22-2013, 03:56 AM
RE: Variables global to a program - Han - 12-22-2013, 04:43 AM
RE: Variables global to a program - Han - 12-22-2013 02:06 PM
RE: Variables global to a program - Snorre - 12-22-2013, 11:56 AM
RE: Variables global to a program - Han - 12-22-2013, 03:52 PM



User(s) browsing this thread: 1 Guest(s)