Sun Studio 12: Fortran Library Reference Reallocate Memory: realloc

The realloc() f95 intrinsic function is called by:

k = realloc(ptr, n )




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




Requested new size of block, in bytes. 

Return value 




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.