Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Deallocation of array with allocated elements

mattsdad
Beginner
730 Views
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?
0 Kudos
3 Replies
Steven_L_Intel1
Employee
730 Views
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.
0 Kudos
marshall-l-buhl
Beginner
730 Views

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

0 Kudos
Steven_L_Intel1
Employee
730 Views

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.

0 Kudos
Reply