Deallocation of array with allocated elements

Deallocation of array with allocated elements

I have a data structure with allocated (inner) array elements. I also have an allocated (outer) array of these data structures. If I deallocate the outer array, will the inner array elements in the data structure also be dealocated or does that need to be done first to prevent a memory leak?

4 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.
Bild des Benutzers Steve Lionel (Intel)

The inner arrays will be automatically deallocated.  This is one of many advantages of allocatable arrays over pointers.  It should be impossible to create a memory leak with allocatables - this is by design.

Steve

Steve,

A point of clarification.  We have a simulator that we sometimes convert to a MATLAB MEX file.  When the simulator returns to MATLAB, it does not deallocate the allocated arrays because the program does not actually terminate.  This has forced us to write termination routines that deallocate everything explicitly.  Stupid MATLAB...  ;-)

Marshall

Bild des Benutzers Steve Lionel (Intel)

Marshall, your situation seems a bit different from what mattsdad asked about.

I assume that your simulator is a collection of Fortran procedures that you call from MATLAB - are they in a DLL? If allocatable arrays are declared local to a routine (perhaps the entry point to your simulator) and not in a module, then they will be deallocated when that routine exits. Otherwise, you are correct in saying that they need to be deallocated explicitly.  Or you can check on the next entry to see if they are already allocated, and if so, deallocate them first.

Steve

Melden Sie sich an, um einen Kommentar zu hinterlassen.