Search code examples
fortrangfortran

Problem with memory allocation for allocatable array( real type )


I am new in Fortran programming so I need a help about allocatable arrays.

This is my simple code:

PROGRAM MY_SIMPLE_CODE

IMPLICIT NONE

INTEGER :: N_TMP, ALLOC_ERR, DEALLOC_ERR

REAL, ALLOCATABLE, DIMENSION(:) :: P_POT

WRITE( *,* ) "ENTER THE VALUE FOR N_TMP:"
 READ( *,* ) N_TMP

IF ( .NOT. ALLOCATED( P_POT) ) ALLOCATE( P_POT( N_TMP), STATUS = ALLOC_ERR )

IF ( ALLOC_ERR .NE. 0 ) STOP( "ERROR - ALLOCATION P_POT !!!")




IF ( ALLOCATED( P_POT) ) DEALLOCATE( P_POT, STATUS = DEALLOC_ERR )

IF ( DEALLOC_ERR .NE. 0 ) STOP( "ERROR - DEALLOCATION P_POT !!!")

END PROGRAM MY_SIMPLE_CODE

When I cobuild this code I got this error message:

Allocate-object is neither a data pointer nor an allocatable variable

What is wrong with this code?

What kind of tricky stuff can be masked in this simple code?

IDE: Code::Blocks TDM_GCC_5 1 0

OS: Win 10 X64


Solution

  • Just like @Steve said in the comment, the keyword for the status of allocation/deallocation is STAT, not STATUS. The error comes because the compiler doesn't recognize the name and thinks it is a variable.

    Moreover, there is a syntax error because there must be at least a space between the STOP statement and the opening brace (or no braces at all).

    IF ( .NOT. ALLOCATED( P_POT) ) ALLOCATE( P_POT( N_TMP), STAT = ALLOC_ERR )
    IF ( ALLOC_ERR .NE. 0 ) STOP "ERROR - ALLOCATION P_POT !!!"
    !(...)
    IF ( ALLOCATED( P_POT) ) DEALLOCATE( P_POT, STAT = DEALLOC_ERR )
    IF ( DEALLOC_ERR .NE. 0 ) STOP "ERROR - DEALLOCATION P_POT !!!"