Sun Studio 12: Fortran Library Reference

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.