The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Program local variables issue
02-10-2020, 02:25 AM
Post: #1
Program local variables issue
In many HPPL programs I have seen declarations of variables placed outside of the bodies of any programs or functions of the module. Those variables were sometimes preceded by a keyword LOCAL, sometimes they were not. The variables were often initialized.
I am assuming that the goal of this structure was to create variables that had scope limited to the module (equivalent of a static global variable in c/c++).

Personally, I try to avoid such style. I try to declare all variables as locals in the body of the program and pass them to the functions as arguments. If I happen to need a global variable, I export it.

Recently I ran into problems with such a program I was trying to use. It could be ran only once. Every subsequent run was generating an error. I spent some time analyzing what's going on, and here is what I found.

The following program demonstrating the issue:
Code:
LOCAL List:={};
LOCAL Number:=101;
TestVariable:="Variable not exported & not local";
  
EXPORT WHATEVER()
BEGIN
  PRINT();
  // print values after initialization
  PRINT("After variable initialization:");
  PRINT (List);
  PRINT (Number);
  PRINT(TestVariable);
  PRINT(" ");
  // modify variables
  List:={{1,2,3},{4,5,6},{7,8,9}};
  Number:=999;
  TestVariable:="Variable modified";
  // print variable after modification
  PRINT("After modification:");
  PRINT (List);
  PRINT (Number);
  PRINT(TestVariable);
  PRINT(" ");
  // modify variables before exiting program
  List:=-1;
  Number:=0;
  TestVariable:="Totally different variable...";
END;
operates on three variables declared outside of the programs/functions. Two of them are declared as LOCAL, the third one is not (but it's not exported). All variables are initialized to (let's call it) state_1.
Inside the program the values of initialized variables are displayed, then they are modified to state_2, displayed again, and, finally, modified again to state_3 before exiting the program.

Please compile the program and run it. You will see the variable values displayed in order: state_1 and state_2, as it should be.
Then run the program again (without recompiling!). You will see the variable values displayed in order: state_3 and state_2.
Upon second run the variables were not initialized. More on, the supposedly local variables from the previous run somehow survived in the OS and were passed to the program upon subsequent run.

It is my understanding that the variables, as locals, should be destroyed the very moment the program terminates.
After the program returns control to the OS, the variables in question cannot be found in any variable catalog in the calculator menus, which seems to indicate they were not exported.

Can anybody shed some light on this behavior?

(Ran on G2 and simulator, f/w 2020 01 16.)

Darius
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Program local variables issue - DrDarius - 02-10-2020 02:25 AM



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