- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
thanks again
Jeremy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Thanks for all your help
Jeremy
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page