Sun Studio 12: Fortran Library Reference

1.4.35 malloc, malloc64, realloc, free: Allocate/Reallocate/Deallocate Memory

The functions malloc(), malloc64(), and realloc() allocate blocks of memory and return the starting address of the block. The return value can be used to set an INTEGER or Cray-style POINTER variable. realloc() reallocates an existing memory block with a new size. free() deallocates memory blocks allocated by malloc(), malloc64(), or realloc().


Note –

These routines are implemented as intrinsic functions in f95, but were external functions in f77. They should not appear on type declarations in Fortran 95 programs, or on EXTERNAL statements unless you wish to use your own versions. The realloc() routine is only implemented for f95.


Standard-conforming Fortran 95 programs should use ALLOCATE and DEALLOCATE statements on ALLOCATABLE arrays to perform dynamic memory management, and not make direct calls to malloc/realloc/free.

Legacy Fortran 77 programs could use malloc()/malloc64() to assign values to Cray-style POINTER variables, which have the same data representation as INTEGER variables. Cray-style POINTER variables are implemented in f95 to support portability from Fortran 77.

1.4.35.1 Allocate Memory: malloc, malloc64

The malloc() function is called by:

k = malloc( n )

n

INTEGER

Input 

Number of bytes of memory 

Return value 

INTEGER(Cray POINTER)

Output 

k>0: k = address of the start of the block of memory allocated

k=0: Error

 

An INTEGER*8 pointer value is returned when compiled for a 64-bit environment with -m64. See Note below.


Note –

This function is intrinsic in Fortran 95 and was external in Fortran 77. Fortran 77 programs compiled to run in 64-bit environments would declare the malloc() function and the variables receiving its output as INTEGER*8. The function malloc64(3F) was provided to make programs portable between 32-bit and 64-bit environments.

k = malloc64( n )

n

INTEGER*8

Input 

Number of bytes of memory 

Return value 

INTEGER*8

(Cray POINTER)

Output 

k>0: k=address of the start of the block of memory allocated

k=0: Error


These functions allocate an area of memory and return the address of the start of that area. (In a 64-bit environment, this returned byte address may be outside the INTEGER*4 numerical range—the receiving variables must be declared INTEGER*8 to avoid truncation of the memory address.) The region of memory is not initialized in any way, and it should not be assumed to be preset to anything, especially zero!

Example: Code fragment using malloc():


       parameter (NX=1000)
       integer ( p2X, X )
       real*4 X(1)
       …
       p2X = malloc( NX*4 )
       if ( p2X .eq. 0 ) stop ’malloc: cannot allocate’
       do 11 i=1,NX
 11         X(i) = 0.
       …
       end

In the above example, we acquire 4,000 bytes of memory, pointed to by p2X, and initialize it to zero.

1.4.35.2 Reallocate Memory: realloc

The realloc() f95 intrinsic function is called by:

k = realloc(ptr, n )

ptr

INTEGER

Input 

Pointer to existing memory block. (Value returned from a previous malloc() or realloc() call).

n

INTEGER

Input 

Requested new size of block, in bytes. 

Return value 

INTEGER

(Cray POINTER)

Output 

k>0: k=address of the start of the new block of memory allocated

k=0: Error

 

An INTEGER*8 pointer value is returned when compiled for a 64-bit environment with -m64. See Note below.

The realloc() function changes the size of the memory block pointed to by ptr to n bytes and returns a pointer to the (possibly moved) new block. The contents of the memory block will be unchanged up to the lesser of the new and old sizes.

If ptr is zero, realloc() behaves the same as malloc() and allocates a new memory block of size n bytes.

If n is zero and ptr is not zero, the memory block pointed to is made available for further allocation and is returned to the system only upon termination of the application.

Example: Using malloc() and realloc() and Cray-style POINTER variables:


       PARAMETER (nsize=100001)
       POINTER (p2space,space)
       REAL*4 space(1)

       p2space = malloc(4*nsize)
       if(p2space .eq. 0) STOP ’malloc: cannot allocate space’
       ...
       p2space = realloc(p2space, 9*4*nsize)
       if(p2space .eq. 0) STOP ’realloc: cannot reallocate space’
       ...
       CALL free(p2space)
       ...

Note that realloc() is only implemented for f95.

1.4.35.3 free: Deallocate Memory Allocated by Malloc

The subroutine is called by:

call free ( ptr )

ptr

Cray POINTER

Input 

free deallocates a region of memory previously allocated by malloc and realloc(). The region of memory is returned to the memory manager; it is no longer available to the user’s program.

Example: free():


       real x
       pointer ( ptr, x )
       ptr = malloc ( 10000 )
       call free ( ptr )
       end