0

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

5
  • 2
    I don't know about Code::Blocks, but I noticed that you use the STATUS= term in your allocation and deallocation. That's wrong, the keyword is STAT=. Commented Mar 6, 2019 at 23:59
  • It's probably what chw21 said, but why don't you post the error message for us to see? Commented Mar 7, 2019 at 0:18
  • @Monochromatic The question is now complete because I added error message. Commented Mar 7, 2019 at 0:37
  • 1
    You have two errors in your code. First, as pointed out by @chw21, you want to STAT= instead of STATUS. The error is correctly pointing at STATUS. gfortran assumed if is a variable name. Second, the STOP does not use parathesis. Remove those. Commented Mar 7, 2019 at 0:49
  • @Steve I did not got warning from compiler that parathesis are syntax error. Why? Can I get a explanation because I want to know reason for that. Commented Mar 7, 2019 at 11:00

1 Answer 1

1

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 !!!"
Sign up to request clarification or add additional context in comments.

3 Comments

It's not so much that STOP ("x") isn't allowed and must be written as STOP "x", but that STOP( "x" ) isn't allowed. The first is valid, the last isn't (placing of spaces).
This is weird... Fortran is not spaces-sensitive in general, specially with braces.
Not very weird: it's no different (in free form) from having to write real x rather than realx or 100 continue rather than 100continue, etc. if(x) is allowed whereas stop(x) isn't, and the difference is that in the latter case the ( is an integral part of the constant (x) - much as you can't have stop1 instead of stop 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.