A finalizer can be used to clean up any state associated with an object. That includes pointer components. But the programmer must still explicitly code this in the finalizer. And in doing this, he must be aware how the pointer association was established (i.e., whether the target was dynamically allocated or not).
e.g. If you use CLASS(*) to construct a generic list,then you may use the elements of the list as TYPE(A),TYPE(B)....TYPE(C).
Now,you want delete a element of the list use "subroutine delete()". However,if the element which maybe a TYPE(A) argument contains a POINTER, the "subroutine delete" can't delete the POINTER.
On May 9, 11:26 am, Simulate <li.sim...@gmail.com> wrote:
> e.g. If you use CLASS(*) to construct a generic list,then you may use > the elements of the list as TYPE(A),TYPE(B)....TYPE(C).
> Now,you want delete a element of the list use "subroutine delete()". > However,if the element which maybe a TYPE(A) argument contains a > POINTER, the "subroutine delete" can't delete the POINTER.
The finalization will be automatically kicked off during the deallocation of an object if that object is finalizable, regardless how that object is declared. This applies also to the implicit deallocations for allocatable variables. So even for CLASS(*), the unlimited polymorphic, pointer/allocatable, yes that object will be finalized if deallocation happens for it.
I think your question was wrong though: how can delete one element of an array? Did you mean a structure like a linked-list? These are different things.
Simulate <li.sim...@gmail.com> wrote: > e.g. If you use CLASS(*) to construct a generic list,then you may use > the elements of the list as TYPE(A),TYPE(B)....TYPE(C).
> Now,you want delete a element of the list use "subroutine delete()". > However,if the element which maybe a TYPE(A) argument contains a > POINTER, the "subroutine delete" can't delete the POINTER.
Perhaps there is a termnology confusion here. I don't know what it means to "delete" a pointer component. I'd normally guess that you mean to deallocate one, or more precisely, to deallocate its target, but I'm not entirely sure of that in context. You are also talking about deleting an element of the linked list. That usage of "delete" makes sense to me, but whan your same paragraph also talks about deleting pointer components, I wonder whether you might be conflating the two concepts. Deleting a linked list element isn't much like "deleting" a pointer component (whatever the latter would be).
In deleting an element from a linked list, one would normally not want to deallocate the targets of the list "next-element" pointers, as those would be other elements of the list. You *MAY* do such a thing; the language doesn't forbid it. That's just probably not what you would want to do.
A finalizer would be more likely to clean up things within an individual list element. If you had a pointer within a list element that was used as a substitute for an allocatable component, then a finalizer could certainly deallocate its target. (Of course, if you were using finalizers, you'd presumably have a new enough compiler that you could just use allocatable components instead of faking them with pointers.)
In short, it really depends on what *YOU* want the finalizer to do. That's more a question of algorithm than of the language. The language does not restrict you against deallocating the targets of pointer components in a finalizer.
There isn't really a lot special about a final subroutine. It is just a subroutine. There are some limitations on it (but none that seem directly related to the question here). The main thing that is special about a final subroutine is the means by which it gets called.... which you haven't actually mentioned in your posts.
Your example doesn't directly show how a finalizer would come into play anyway. If your "subroutine delete" deallocated a list element, then that list element would be finalized, because deallocation is one of the things that triggers finalization. I suppose that your subroutine delete could itself be a final subroutine; that seems like a strange way to implement a lnked list. I suppose it could be done, but I don't know why one would use a finalizer instead of a normal subroutine call for such a purpose. After all, the other linked list operations would presumably be done with normal subroutine calls.
-- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
Jim Xia <jim...@hotmail.com> wrote: > I think your question was wrong though: how can delete one element of > an array? Did you mean a structure like a linked-list? These are > different things.
Yes... and Simulate's posting did not use the word "array", but it did refer to a linked list. Well, it didn't use the word "linked", but I think it implied. I'm supposing that you must have misread it somehow. Perhaps you took the term "element" to imply an array, but Simulate did say "element of the list".
-- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
type :: genlist class(*), pointer :: stuff type(genlist), pointer :: next => null() end type
in mind. Deleting an element will (in this simplified scenario) require something like
subroutine delete_after(this) type(genlist) :: this if (associated(this%next) then deallocate(this%next%stuff) deallocate(this%next) end if end subroutine
where the actual argument provided must be the next-to-last element. This also assumes we're talking about value (as opposed to reference) containers. Assuming the "stuff" you put in is itself decorated with pointer components: That is where the finalizer comes in. It will be automatically executed during the deallocate(this%next%stuff) before the deallocation of stuff itself is actually performed.
> e.g. If you use CLASS(*) to construct a generic list,then you may use > the elements of the list as TYPE(A),TYPE(B)....TYPE(C).
> Now,you want delete a element of the list use "subroutine delete()". > However,if the element which maybe a TYPE(A) argument contains a > POINTER, the "subroutine delete" can't delete the POINTER.
On May 9, 12:03 pm, nos...@see.signature (Richard Maine) wrote:
> Jim Xia <jim...@hotmail.com> wrote: > > I think your question was wrong though: how can delete one element of > > an array? Did you mean a structure like a linked-list? These are > > different things.
> Yes... and Simulate's posting did not use the word "array", but it did > refer to a linked list.
Well, I may be wrong but the word "element" does imply an array. This may seem to be an obviously wrong question to you but not to C/C++ programmers. I was asked about similar questions a few time by C++ programmers. This was what C++ programmers have in their mind about an array:
T * array[NO_OF_ELEMENT];
then the question was all making sense: array[0] = &obj1; // of type(A) array[1] = &obj2; // of type(B) ...
Yes, you can delete an element of an array in C++: delete array[0]. Nothing wrong here.
The OO stuff in Fortran inevitably draws some attention from other languages and we need to explain the difference in Fortran arrays from arrays in other languages. Otherwise the confusion will be there forever.
> Yes... and Simulate's posting did not use the word "array", but it did > refer to a linked list. Well, it didn't use the word "linked", but I > think it implied.
Well, I may be wrong, but the word "element" does imply an array. The question about delete an element of an array may seem an obviously wrong question to you, but not to C/C++ programmers. I was asked a few times on similar questions by C++ programmers. This is what an array means for C++ programmers:
T* array[NO_OF_ELEMENTS];
array[0] = &obj1; // of type A array[1] = &obj2; // of type B
And yes you can delete an element in an array: delete array[1]. It's perfectly legal in C++. And I was trying to clarify if the previous question was talking about this.
The OO stuff in Fortran will inevitably attract some attention from other languages. And we'd better explain clearly the difference between Fortran arrays and arrays in other languages. Otherwise the "confusing" questions will just keep coming.
Jim Xia wrote: >> Yes... and Simulate's posting did not use the word "array", but it >> did refer to a linked list. Well, it didn't use the word "linked", >> but I think it implied.
> Well, I may be wrong, but the word "element" does imply an array. > [...]
Arrays do have elements. And since Fortran has become an array- based language, there are a lot of mentions of array elements in the standard document. However, I think you'll find that almost all such uses of the word are qualified as "array elements", not just "elements" with the expection you will assume an array is involved. There is, in fact, a definition of "scalar" which states that it's a single element. (Of course, since scalars can be regarded as arrays of rank zero I suppose you could still make the claim that all uses of the word "element" refer to arrays.) I couldn't find any uses of the word "element" that assumed arrays were involved that didn't include *some* mention of that in the relevant context.
I can find no formal definition of just the unqualified word "element" that constrains it to refer only to data within arrays. Informally, people often refer to list elements, tree elements, etc. It seems to me that a single datum from *any* collection might be informally referred to as an element.
-- J. Giles
"I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies." -- C. A. R. Hoare
Jim Xia wrote: >>Yes... and Simulate's posting did not use the word "array", but it did >>refer to a linked list. Well, it didn't use the word "linked", but I >>think it implied. > Well, I may be wrong, but the word "element" does imply an array. The > question about delete an element of an array may seem an obviously > wrong question to you, but not to C/C++ programmers. I was asked a > few times on similar questions by C++ programmers. This is what an > array means for C++ programmers:
I believe that linked lists are often considered elements. (The wikipedia entry seems to call them nodes.)
I believe it is more general than just arrays, though.
More specifically, in AWK associative arrays you can delete array elements with the delete statement. (Array elements are created when referenced.) One can delete an individual element or all the elements at once. (AWK only has associative arrays, I believe they are one of the array types in PERL.)
I was once in a long discussion on whether the things that make up structures could be considered elements. I won't say which side I was on, though, unless someone wants to start that discussion here.