Node: %LOC(), Up: Expressions

The %LOC() Construct


The %LOC() construct is an expression that yields the value of the location of its argument, arg, in memory. The size of the type of the expression depends on the system--typically, it is equivalent to either INTEGER(KIND=1) or INTEGER(KIND=2), though it is actually type INTEGER(KIND=7).

The argument to %LOC() must be suitable as the left-hand side of an assignment statement. That is, it may not be a general expression involving operators such as addition, subtraction, and so on, nor may it be a constant.

Use of %LOC() is recommended only for code that is accessing facilities outside of GNU Fortran, such as operating system or windowing facilities. It is best to constrain such uses to isolated portions of a program--portions that deal specifically and exclusively with low-level, system-dependent facilities. Such portions might well provide a portable interface for use by the program as a whole, but are themselves not portable, and should be thoroughly tested each time they are rebuilt using a new compiler or version of a compiler.

Do not depend on %LOC() returning a pointer that can be safely used to define (change) the argument. While this might work in some circumstances, it is hard to predict whether it will continue to work when a program (that works using this unsafe behavior) is recompiled using different command-line options or a different version of g77.

Generally, %LOC() is safe when used as an argument to a procedure that makes use of the value of the corresponding dummy argument only during its activation, and only when such use is restricted to referencing (reading) the value of the argument to %LOC().

Implementation Note: Currently, g77 passes arguments (those not passed using a construct such as %VAL()) by reference or descriptor, depending on the type of the actual argument. Thus, given INTEGER I, CALL FOO(I) would seem to mean the same thing as CALL FOO(%VAL(%LOC(I))), and in fact might compile to identical code.

However, CALL FOO(%VAL(%LOC(I))) emphatically means "pass, by value, the address of I in memory". While CALL FOO(I) might use that same approach in a particular version of g77, another version or compiler might choose a different implementation, such as copy-in/copy-out, to effect the desired behavior--and which will therefore not necessarily compile to the same code as would CALL FOO(%VAL(%LOC(I))) using the same version or compiler.

See Debugging and Interfacing, for detailed information on how this particular version of g77 implements various constructs.