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”
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.