Next: MATMUL, Previous: LTIME, Up: Intrinsic Procedures
MALLOC
— Allocate dynamic memoryMALLOC(SIZE)
allocates SIZE bytes of dynamic memory and
returns the address of the allocated memory. The MALLOC
intrinsic
is an extension intended to be used with Cray pointers, and is provided
in GNU Fortran to allow the user to compile legacy code. For new code
using Fortran 95 pointers, the memory allocation intrinsic is
ALLOCATE
.
PTR = MALLOC(SIZE)
SIZE | The type shall be INTEGER(*) .
|
INTEGER(K)
, with K such that
variables of type INTEGER(K)
have the same size as
C pointers (sizeof(void *)
).
MALLOC
and
FREE
with Cray pointers. This example is intended to run on
32-bit systems, where the default integer kind is suitable to store
pointers; on 64-bit systems, ptr_x would need to be declared as
integer(kind=8)
.
program test_malloc integer i integer ptr_x real*8 x(*), z pointer(ptr_x,x) ptr_x = malloc(20*8) do i = 1, 20 x(i) = sqrt(1.0d0 / i) end do z = 0 do i = 1, 20 z = z + x(i) print *, z end do call free(ptr_x) end program test_malloc