Clicky

Fortran Wiki
Multiple allocation, smart pointer

Smart pointers as an extension of allocatable variables. Basically the same memory area is referenced by different variables but get automatically deallocated only when no smart pointers reference it any more. The real deallocation is performed only when all the smart pointers pointing to it are deallocated (or get out of scope). A keyword as “multiallocatable” may be used plus a function like add_alloc that add an allocation to a multiallocatable variable. For example

TYPE(xxx), multiallocatable :: vara, varb ! two variables declared multiallocatable

allocate(vara) the first variable is allocated

call add_alloc(vara, varb) ! now vara and varb point to the same area and are alias

deallocate(vara) ! vara is now deallocated but varb is still valid and allocated

deallocate(varb) ! only now the memory is really deallocated given back to the operating system (or the memory pool of the compiler) ready for other allocations.