Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28712 Discussions

deallocattion on function return

jft27
Beginner
457 Views
Hi,
I know that the recent versions of the compiler support automatic deallocation of arrays on exiting a function but I am wondering if this works okay if I wish the return value of a function to be a derived type which contains an allocatable array as an element of the type?
many thanks
Jeremy
0 Kudos
4 Replies
Steven_L_Intel1
Employee
457 Views
The automatic deallocation you refer to applies only to local variables that are allocatable or contain allocatable components. It does not apply to function return values which live (for a short time) after the function returns. If you have a function return with an allocatable component, it will get deallocated eventually by the caller.
0 Kudos
jft27
Beginner
457 Views
Thanks for the reply. So is it considered a bad idea to make use of this "feature"? Is there an advantage to using pointer arrays instead of allocatable arrays in my derived type?
thanks again
Jeremy
0 Kudos
Steven_L_Intel1
Employee
457 Views
I would in general recommend use of ALLOCATABLE instead of POINTER but there are some subtle differences (especially with components of derived type and assignment) that you need to be aware of. Personally, I would avoid either in function returns and would suggest using a passed argument for these things rather than a function return. Dealing with allocatable components in a function return is quite tricky for the compiler and is also difficult for the human to understand the fine details.
0 Kudos
jft27
Beginner
457 Views
Okay. I had been wanting to use this in order to define overloaded arithmetic operators for a couple of rather ugly derivied types in my code in the hope of making it look a little prettier, however you're right it'll probably make it baffling to read and I should go about it differently.
Thanks for all your help
Jeremy
0 Kudos
Reply