Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect undefined real variables?

156 views
Skip to first unread message

Peter

unread,
Jan 31, 2006, 2:26:37 PM1/31/06
to
To detect use of undefined real variables, I'd like to have the
compiler or linker initialize all real variables with a value such that
if it is used in an assignment or expression, it causes an exception,
or otherwise sends a signal.

Is this possible? I'm using Compaq Visual Fortran 6.0.

Richard E Maine

unread,
Jan 31, 2006, 3:15:24 PM1/31/06
to
Peter <sher...@programmer.net> wrote:

Probably, though I don't have a manual handy to look it up in.

Do note that this would not be adequate to catch all cases of undefined
variables, though it certainly would catch a large and important class
of them. The f95 standard lists 15 things that cause variables to become
undefined (section 14.7.6) even if they were previously defined.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain

Richard Edgar

unread,
Jan 31, 2006, 3:15:32 PM1/31/06
to

I'm not sure about CVF in particular, but finding the compiler option to
force allocation on the stack would be a good start. For the Sun
compiler, this is -stackvar, for the Intel compiler, it's -auto. If CVF
has similar options to the HP compiler, then the switch will be -automatic.

Doing this tends to mean that undefined variables will get random values
(those left on the stack by other procedures), rather than the 'helpful'
initialisation to zero which compilers often do otherwise. This means
that your code will crash with obviously 'funny' values, rather than
ploughing on with something which isn't obviously wrong. However, it's
no absolute guarantee, and doesn't help with SAVE'd variables.

There might also be a compiler switch in CVF to check for this
specifically, but the compiler probably can't catch all cases. Making
sure that you have explicit interfaces with INTENT defined should help,
though.

HTH,

Richard

Colin Watters

unread,
Feb 1, 2006, 3:56:35 PM2/1/06
to
Salford can do this, alas Compaq cannot. See www.polyhedron.co.uk (looks
like Lahey can do it too).

--
Qolin

Email: my qname at domain
Domain: qomputing dot demon dot co dot uk
"Peter" <sher...@programmer.net> wrote in message
news:1138735597.1...@z14g2000cwz.googlegroups.com...

glen herrmannsfeldt

unread,
Jan 31, 2006, 10:08:49 PM1/31/06
to
Peter wrote:

IEEE floating point includes a signaling NAN for this purpose.
Whether or not it is implemented in your system, I don't know.
Also, the ability to initialize all variables to a specified value
would be system dependent, and rare. You could initialize them
with a BOZ constant, again system dependent.

-- glen

John Harper

unread,
Jan 31, 2006, 11:16:43 PM1/31/06
to
In article <KrOdnYcuIcz...@comcast.com>,

glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
>Peter wrote:
>
>> To detect use of undefined real variables, I'd like to have the
>> compiler or linker initialize all real variables ...

>> Is this possible? I'm using Compaq Visual Fortran 6.0.
>
>Also, the ability to initialize all variables to a specified value
>would be system dependent, and rare.

G95 has an option -freal= in which you may put after the = any
one of none, zero, nan, inf, +inf, or -inf. It initializes all
uninitialized scalar real and complex variables to the value
specified.
--
John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington, New Zealand
e-mail john....@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045


Jugoslav Dujic

unread,
Feb 1, 2006, 3:03:49 AM2/1/06
to
Colin Watters wrote:
| Email: my qname at domain
| Domain: qomputing dot demon dot co dot uk
| "Peter" <sher...@programmer.net> wrote in message
| news:1138735597.1...@z14g2000cwz.googlegroups.com...
|| To detect use of undefined real variables, I'd like to have the
|| compiler or linker initialize all real variables with a value such that
|| if it is used in an assignment or expression, it causes an exception,
|| or otherwise sends a signal.
||
|| Is this possible? I'm using Compaq Visual Fortran 6.0.
|
| Salford can do this, alas Compaq cannot. See www.polyhedron.co.uk (looks
| like Lahey can do it too).

Correct. About the best one can get with CVF is to follow R. Edgar's advice
and compile with /automatic (there's an appropriate check-box somewhere
in IDE Project Settings).

In addition, I made a low-level hack "overriding" compiler's default
ALLOCATE routine from the run-time library. The hack routine
fills the allocated memory with repeating bit-pattern Z'7FF0', which
evaluates to NaN for both IEEE single and double, and large positive
for 4-byte integers. Of course, it "undefines" only the allocated
arrays. I can post the code if anyone is interested.

--
Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.

Colin Watters

unread,
Feb 2, 2006, 3:30:49 AM2/2/06
to
I'm interested!

--
Qolin

Email: my qname at domain
Domain: qomputing dot demon dot co dot uk

"Jugoslav Dujic" <jdu...@yahoo.com> wrote in message
news:44b8hhF...@individual.net...

David Flower

unread,
Feb 1, 2006, 3:40:37 AM2/1/06
to

The Salford FTN77 and FTN95 both have the feature, which works well.

I have always felt that onhe of the best tests of a program is to get
it running under more than one compiler; if this is an option, I
suggest trying it

David Flower

Keith Refson

unread,
Feb 1, 2006, 4:26:08 AM2/1/06
to

Others have answered that CVF does not have this capability.

At the risk of irritating the OP by pointing out a solution which is
not applicable to him on the Windows platform, I'd like to mention Valgrind
which runs on any x86/linux platform. The website is

http://www.valgrind.org/

but it comes with many linux distributions too. It does very comprehensive
analysis of unassigned variables, bounds errors and memory leak detection,
and can be used with many compilers. It provides stack tracebacks with source
line numbers provided that your compiler can generate debugging symbols
in the common formats. I have used this with both Intel and g95 fortran
compilers to good effect.

There are some nice GUI frontends mentioned on the Valgrid website. I like
Valkyrie.

A word of warning. Valgrind will probably reveal bugs is your compiler's
run-time library and in O/S routines as well as your program! It sometimes
needs a little work using the mechanisms provided to suppress "harmless"
errors before you narrow down to the significant ones. And of course it
does increase execution time considerably, but not IMO intolerably.

Keith Refson

--
Dr Keith Refson,
Building R3
Rutherford Appleton Laboratory
Chilton
Didcot kr AT
Oxfordshire OX11 0QX isise D@T rl D.T ac D?T uk

Jugoslav Dujic

unread,
Feb 1, 2006, 6:25:05 AM2/1/06
to
| I'm interested!

OK, here it is. It replaces C routine for_alloc_allocatable from CVF
run-time library. Copy the contents to a *separate* source file and
insert it into the project. In order to make the linker happy, you have to
*manually* append /force:multiple to Project/Settings/Link/Project options.
As result, you will get linker warnings about multiple uses of the symbol,
which you can ignore. I do this only for debug configuration, and I
"exclude file from build" for the release.

Basically, it's C-like code, happened to be written in Fortran using CVF
extensions. It should work for IVF as well, but I didn't test it.
I'm not sure what happens if you link with dll version of run-time
library (I use /libs:static); I think the same, i.e. the "hacked"
version takes precedence, but you should verify it:

integer function for_alloc_allocatable(nBytes,lpAddress,iSilentErr) result(ierr)
!DEC$ATTRIBUTES C:: for_alloc_allocatable
!DEC$DEFINE ALLOCATE_NAN

use dfwin, only: RaiseException, EXCEPTION_ACCESS_VIOLATION

implicit none

integer, intent(in):: nBytes
integer:: lpAddress
!DEC$ATTRIBUTES REFERENCE:: lpAddress
integer, intent(in):: iSilentErr

!Interface to RTL default allocator function (RTL for_alloc_allocatable is
a mere wrapper with error handling)
interface
integer function for_allocate(nBytes,lpAddress,iSilentErr)
!DEC$ATTRIBUTES C, ALIAS: "_for_allocate":: for_allocate
integer:: nBytes
integer:: lpAddress
!DEC$ATTRIBUTES REFERENCE:: lpAddress
integer:: iSilentErr
end function for_allocate
end interface

integer:: i
integer(1), parameter:: MAGIC_NAN1(2)=INT((/Z'F0',Z'7F'/),1)

integer(1):: iArray(*); pointer(pArray, iArray)

if (lpAddress.ne.0) then
ierr = 151 !Array already allocated
return
end if

ierr = for_allocate(nBytes,lpAddress,1)

if (ierr.eq.0) then
!Hack #1 -- fill the memory with MAGIC_NAN1
!DEC$IF DEFINED(ALLOCATE_NAN)

pArray = lpAddress

!Unrolled loop for efficiency
do i=1,nBytes,4
iArray(i:i+3) = (/MAGIC_NAN1(1), MAGIC_NAN1(2), MAGIC_NAN1(1),
MAGIC_NAN1(2)/)
end do
do i=i,nBytes
iArray(i) = MAGIC_NAN1(1)
end do
!DEC$ENDIF
else if (iSilentErr.eq.0) then
!Hack #2 -- if an error occurs AND stat= is not specified,
!raise an exception instead of #$%|#$ default message box:
call RaiseException(EXCEPTION_ACCESS_VIOLATION, 0, 0, 0)
end if

end function for_alloc_allocatable

Herman D. Knoble

unread,
Feb 1, 2006, 8:58:00 AM2/1/06
to
Peter: There are really only two Fortran 90/95
compilers that detect uninitialized variables AT RUN-TIME:

Salford (Silverfrost) FTN95 with Windows options:
/DEBUG /CHECK /FULL_UNDEF /FULL_DEBUG

There is a no-cost personal edition of ftn95 available at:
http://www.silverfrost.com/32/ftn95/ftn95_personal_edition.asp
and
http://www.polyhedron.co.uk/salford/products/other/ftn95_personal.html


Lahey LF95 with Windows options:
-g -chk -chkglobal -nsav -trace -stchk -pca -trap diou

or Linux LF95 options:
-g --chk --chkglobal --nsav --trace --pca

Lahey offers lf95 at an Express academic (not for profit) price
at a fraction of the commercial cost. See:
http://www.lahey.com/pricelst.pdf

As another poster suggested see the Polyhedron site. They meant
the compiler diagnostic table at:
Windows compilers: http://www.polyhedron.com/pb05/win32/diagnose.html
Linux Compilers: http://www.polyhedron.com/pb05/linux/diagnose.html
(scroll down to the UNinitialized section).


Skip Knoble

On 31 Jan 2006 11:26:37 -0800, "Peter" <sher...@programmer.net> wrote:

-|To detect use of undefined real variables, I'd like to have the
-|compiler or linker initialize all real variables with a value such that
-|if it is used in an assignment or expression, it causes an exception,
-|or otherwise sends a signal.
-|
-|Is this possible? I'm using Compaq Visual Fortran 6.0.

Paul Van Delst

unread,
Feb 1, 2006, 10:08:13 AM2/1/06
to

Not just that - different platforms too if possible (especially if you have I/O code that
may be sensitive to endian-ness). While I don't advocate using multi-compilers as a
debugging tool, it sure helps. :o)

cheers,

paulv


--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC

Richard Edgar

unread,
Feb 1, 2006, 10:56:34 AM2/1/06
to
Paul Van Delst wrote:

>> I have always felt that onhe of the best tests of a program is to get
>> it running under more than one compiler;
>
> Not just that - different platforms too if possible (especially if you
> have I/O code that may be sensitive to endian-ness). While I don't
> advocate using multi-compilers as a debugging tool, it sure helps. :o)

Having had a few bad experiences, I'd go so far as to say that I don't
trust codes which don't compile on multiple platforms with multiple
compilers. Certainly, changing platform and compiler has caught bugs in
my code, which the original compiler had silently overlooked.

Richard

0 new messages