Allocated arrays not initialized to zero after deallocate

I have traced down a problem I have been having in a code compiled with pgf90 (6.2) to an apparent issue with allocating arrays after deallocate has been used. I have two main modules in this code. The first module allocates a large amount of memory to process some input from data files into a file format compatible with the second module. After the first module is done, the arrays in that module are deallocated. When the second module starts, a new set of arrays are allocated to work in that part of the code. What I have found is that, by using the deallocate statements in the first module, many of my new arrays in the second module are not initialized to zero when they are allocated - values from arrays that were deallocated show up in random ways in the new arrays. If I remove the deallocate statements from the first module, all of the new allocated arrays in the second module are initialized to zero. Is this normal? Do I need to make sure I initialize every single array I use, or is there a compiler flag that will take care of this? I am running this code on a dual-processor AMD-64 bit system running SUSE Linux 9.3. Thanks in advance!

Hi Dudley,

Memory returned by ALLOCATE is never intialized. It may happen to contain zeros, but is just as likely to contain junk. It’s good practice to initialize your data and fortunately, Fortran makes this very easy to do. Simply assign the initial value to the array or use a data statement.

If I remove the deallocate statements from the first module, all of the new allocated arrays in the second module are initialized to zero. Is this normal?

That’s what happens to be in memory. It could be just a easily be junk. Note by removing the deallcate, you’ve created a memory leak in your program and may cause other issue such as running out of memory.

Do I need to make sure I initialize every single array I use,

Depends on your program but in my opinion it’s a good practice to adopt.

or is there a compiler flag that will take care of this?

Sorry, no such flag.

  • Mat