Operating system error: Cannot allocate memory

I am trying to write a test for a subroutine and I have a variable called flags defined as :

character(len=:), allocatable :: flags

flags is passed as an argument to the subroutine that populates its value. When I try to write the statement write(*,*) flags inside the subroutine for debugging purposes.

I am getting this error:

Operating system error: Cannot allocate memory
Memory allocation failure in xrealloc

PS: This subroutine does not throw this error during the normal execution of the program which means when it is not called inside the test suite.

After reading your source code, I think the issue is near line 42.

1 Like

That’s @kargl’s-somewhat-sarcastic way of saying that we need to see your code, at least the subroutine in question and the line that calls it, in addition to just a declaration of flags

1 Like

Being that 42 is The Answer to Life The Universe and Everything; it’s probably around there somewhere.

I would just like to add that you might want to do a “write(*,*)allocated(flag)” and “write(*,*)len(flag)” before printing it, as the most likely cause for running out of memory would be printing a string whose length is undefined or defined with a garbage value that is gigantic; and using an unallocated string in a WRITE statement has no defined behavior as far as the standard is concerned that I know of; so any particular compiler you are using could do just about anything, almost like
some Infinite Improbability Drive.

another missing piece of information that is often helpful is what compiler, compiler version, and OS you are using; and the obscure references are to “A Hitchhiker’s Guide to the Galaxy” :wink:

1 Like

He did say it was a subroutine. It’s unclear if this is an intent(out) dummy argument. Bad things may ensue if it is.

1 Like

Hi everyone,

I was able to resolve this issue. It was due to the reason that @urbanjost mentioned. I was trying to access unallocated flags variable inside my subroutine.

In the normal execution of code, the flags variable was allocated and populated with some flags before calling the subroutine, but while writing a test, the flags variable was not allocated, so I had to change the logic in my subroutine to test it better.

I regret I should have shared the code snippet in detail and compiler information while asking the question. Will keep this in mind while asking questions in the future.

This is a common programmer error, and it could be largely eliminated if the language would allow allocatable variables to be initialized at declaration. It would be useful to initialize them to an allocated state with some value, and for characters and arrays, also of zero size.


@RonShepard , if you have not done so already, can you please post a thread at J3 Fortran proposals site with a summary of your thoughts on objects with the ALLOCATABLE attribute?

It can then be brought to the attention of the Fortran committee for consideration toward Fortran 202Y revision.