1

I am running a FORTRAN program that allocates memory dynamically for rather large arrays, and sometimes they do not fit into memory.

Thus the allocation

 allocate(my_array(really big_number))

will give the error

Operating system error: Cannot allocate memory
Allocation would exceed memory limit

and the program would exit. I would like to know if there is any way to capture this or test that the memory will be available, so that i can take appropriate measures if I'm not allowed to allocate such a big array?

1 Answer 1

1

Use: allocate(my_array(really big_number),stat=ierror)

With the stat= specifier, the status of the allocation will be stored in the specified variable (ierror in the example). Zero means the allocation succeeded, non-zero means it failed.

From the Fortran 90 standard (ftp://ftp.nag.co.uk/sc22wg5/N001-N1100/N692.pdf) on the ALLOCATE statement:

If the STAT= specifier is present, successful execution of the ALLOCATE statement causes the stat-variable to become defined with a value of zero.

If an error condition occurs during the execution of the ALLOCATE statement, the stat-variable becomes defined with a processor-dependent positive integer value. If an error condition occurs during execution of an ALLOCATE statement that does not contain the STAT= specifier, execution of the executable program is terminated.

Sign up to request clarification or add additional context in comments.

1 Comment

Worth to note it is not bomb proof. En error can still be encountered when accessing the array on some systems.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.