Deallocation of array with allocated elements

Deallocation of array with allocated elements

Portrait de mattsdad

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 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.
Portrait de 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
Portrait de marshall-l-buhl

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

Portrait de 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

Connectez-vous pour laisser un commentaire.