Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test for intentionally undefined variables

Status
Not open for further replies.

hdalgarno

Programmer
Jun 27, 2013
2
0
0
GB
(I am using F90/95 with the FTN95 Silverfrost compiler)

Is it possible to test for an undefined variable, that is intentionally undefined unless some event has occured?

For example, lets say I define X to be an integer but give it no initial value. During the course of the program X will be given a value ONLY if an event Y occurs. Later, I would like to initiate one set of commands if event Y has occured, and another set if it has not occured. I'd like to know whether I can test X to ask if it is undefined, or if it has a value, and use the outcome of this test to select which set of commands to initiate.

I realise that I could use a flag that is set when event Y occurs but I have limited flexibility to introduce new variables to this particular application, so an ability to test the existing variable X would be the preferred first solution.

I'd really appreciate any advice on this
 
Select project properties/debugging/check for undefined variables. Change the setting to yes. Alternatively, if you are running from cmd line, add /UNDEF.
 
If xwb's tip doesn't work with your compiler, you may have another option. Do you know anything about the possible values that X could be initialized to? For example, the logic of your code might only allow X to be a positive number. If this is the case, you could initialize X to a negative number. Then you could do something like this:

Code:
if (X < 0.) then
    ! X has not been defined in the normal program flow
else
    ! X has been defined at some point
end if
 
Dear Fivetran, Many thanks for this - this was indeed the solution I chose to use in the end. X represented a time value in my code, so initialising to a large negative value was clearly a 'nonsense' value that I could then test for. It took a little extra work to make sure all instances of X throughout the model (which is very large!) were carefully checked to ensure that setting this value wouldn't have any impact on other sections of code, and in the end this has been a very effective solution :)
 
An uninitialized variable is exactly that: it may be any value. If you are running from VS in debug mode, all bytes are set to CC so you will get large negative values. If you are running in release mode, this is not guaranteed to happen. You'll get whatever happens to be in memory. You may find that negative values work sometimes and not at other times. The best way is to set it at the start of the program and check it for the start value at the end instead of relying on it to be a large negative value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top