Manual Reference Pages  - transfer (3fortran)

NAME

TRANSFER(3) - [TYPE:MOLD] Transfer bit patterns

SYNOPSIS

result = transfer(source, mold [,size] )

         type(TYPE(kind=KIND)) function transfer(source,mold,size)

type(TYPE(kind=KIND)),intent(in) :: source(..) type(TYPE(kind=KIND)),intent(in) :: mold(..) integer(kind=**),intent(in),optional :: size

CHARACTERISTICS

o SOURCE shall be a scalar or an array of any type.
o MOLD shall be a scalar or an array of any type.
o SIZE shall be a scalar of type integer.
o RESULT has the same type as MOLD

DESCRIPTION

TRANSFER(3) copies the bitwise representation of SOURCE in memory into a variable or array of the same type and type parameters as MOLD.

This is approximately equivalent to the C concept of "casting" one type to another.

OPTIONS

o SOURCE : Holds the bit pattern to be copied
o MOLD : the type of MOLD is used to define the type of the returned value. In addition, if it is an array the returned value is a one-dimensional array. If it is a scalar the returned value is a scalar.
o SIZE : If SIZE is present, the result is a one-dimensional array of length SIZE.
If SIZE is absent but MOLD is an array (of any size or shape), the result is a one-dimensional array of the minimum length needed to contain the entirety of the bitwise representation of SOURCE.

If SIZE is absent and MOLD is a scalar, the result is a scalar.

RESULT

The result has the bit level representation of SOURCE.

If the bitwise representation of the result is longer than that of SOURCE, then the leading bits of the result correspond to those of SOURCE but any trailing bits are filled arbitrarily.

When the resulting bit representation does not correspond to a valid representation of a variable of the same type as MOLD, the results are undefined, and subsequent operations on the result cannot be guaranteed to produce sensible behavior. For example, it is possible to create logical variables for which VAR and .not. var both appear to be true.

EXAMPLES

Sample program:

    program demo_transfer
    use,intrinsic :: iso_fortran_env, only : int32, real32
    integer(kind=int32) :: i = 2143289344
    real(kind=real32)   :: x
    character(len=10)   :: string
    character(len=1)    :: chars(10)
       x=transfer(i, 1.0)    ! prints "nan" on i686
       ! the bit patterns are the same
       write(*,’(b0,1x,g0)’)x,x ! create a NaN
       write(*,’(b0,1x,g0)’)i,i

! a string to an array of characters string=’abcdefghij’ chars=transfer(string,chars) write(*,’(*("[",a,"]":,1x))’)string write(*,’(*("[",a,"]":,1x))’)chars end program demo_transfer

Results:

       1111111110000000000000000000000 NaN
       1111111110000000000000000000000 2143289344
       [abcdefghij]
       [a] [b] [c] [d] [e] [f] [g] [h] [i] [j]

COMMENTS

Joe Krahn: Fortran uses MOLDING rather than CASTING.

Casting, as in C, is an in-place reinterpretation. A cast is a device that is built around an object to change its shape.

Fortran TRANSFER(3) reinterprets data out-of-place. It can be considered MOLDING rather than casting. A MOLD is a device that confers a shape onto an object placed into it.

The advantage of molding is that data is always valid in the context of the variable that holds it. For many cases, a decent compiler should optimize TRANSFER(3) into a simple assignment.

There are disadvantages of this approach. It is problematic to define a union of data types because you must know the largest data object, which can vary by compiler or compile options. In many cases, an EQUIVALENCE would be far more effective, but Fortran Standards committees seem oblivious to the benefits of EQUIVALENCE when used sparingly.

STANDARD

Fortran 90

SEE ALSO

****(3)

fortran-lang intrinsic descriptions


Nemo Release 3.1 transfer (3fortran) April 28, 2024
Generated by manServer 1.08 from 49fbf7f9-9d90-489d-925e-e7b1bc02f25a using man macros.