Automatic array deallocation in Fortran

后端 未结 2 1254
闹比i
闹比i 2021-01-05 07:03

I\'m using gfortran -std=f2008. I have a function that returns a derived type which contains an allocatable array. The function calls allocate() before it returns. It see

2条回答
  •  梦毁少年i
    2021-01-05 07:36

    A universal answer: allocatable arrays are automatically deallocated when going out of scope. Function results are automatically deallocated after the result is "used" in the outer scope. Allocatable components are deallocated, when the parent derived type variable is going out of scope, or when it is deallocated.

    Only pointers, that pointed to the function results are undefined after the deallocation and shall not be used. If you do that, it could cause the problems you describe.

    Also, when an array is automatically reallocated on assignment, the pointers to it become undefined (but may not change, actually).

    In other words, problems you describe shouldn't occur when the allocatables are used correctly.

提交回复
热议问题