Google Groups Home
Help | Sign in
Can "Final subroutines" in F2003 delete pointer components in a class?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Simulate  
View profile
 More options May 9, 5:51 am
Newsgroups: comp.lang.fortran
From: Simulate <li.sim...@gmail.com>
Date: Fri, 9 May 2008 02:51:35 -0700 (PDT)
Local: Fri, May 9 2008 5:51 am
Subject: Can "Final subroutines" in F2003 delete pointer components in a class?
or the pointer components must be deleted by programmers themselves?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reinhold Bader  
View profile
 More options May 9, 10:51 am
Newsgroups: comp.lang.fortran
From: Reinhold Bader <Ba...@lrz.de>
Date: Fri, 09 May 2008 16:51:05 +0200
Local: Fri, May 9 2008 10:51 am
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?
Hello,

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).

Regards

Simulate schrieb:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simulate  
View profile
 More options May 9, 11:26 am
Newsgroups: comp.lang.fortran
From: Simulate <li.sim...@gmail.com>
Date: Fri, 9 May 2008 08:26:00 -0700 (PDT)
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?
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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Xia  
View profile
 More options May 9, 11:37 am
Newsgroups: comp.lang.fortran
From: Jim Xia <jim...@hotmail.com>
Date: Fri, 9 May 2008 08:37:56 -0700 (PDT)
Local: Fri, May 9 2008 11:37 am
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?
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.

Cheers,

Jim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Can "Final subroutines" in F2003 delete pointer components in a class?" by Richard Maine
Richard Maine  
View profile
 More options May 9, 11:55 am
Newsgroups: comp.lang.fortran
From: nos...@see.signature (Richard Maine)
Date: Fri, 9 May 2008 08:55:21 -0700
Local: Fri, May 9 2008 11:55 am
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Maine  
View profile
 More options May 9, 12:03 pm
Newsgroups: comp.lang.fortran
From: nos...@see.signature (Richard Maine)
Date: Fri, 9 May 2008 09:03:55 -0700
Local: Fri, May 9 2008 12:03 pm
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Can "Final subroutines" in F2003 delete pointer components in a class?" by Reinhold Bader
Reinhold Bader  
View profile
 More options May 9, 1:13 pm
Newsgroups: comp.lang.fortran
From: Reinhold Bader <Ba...@lrz.de>
Date: Fri, 09 May 2008 19:13:05 +0200
Local: Fri, May 9 2008 1:13 pm
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?
I assume you have a type definition like

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.

Regards

Simulate schrieb:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Xia  
View profile
 More options May 9, 1:19 pm
Newsgroups: comp.lang.fortran
From: Jim Xia <jim...@hotmail.com>
Date: Fri, 9 May 2008 10:19:57 -0700 (PDT)
Local: Fri, May 9 2008 1:19 pm
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?
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.

Cheers,

Jim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Xia  
View profile
 More options May 9, 1:32 pm
Newsgroups: comp.lang.fortran
From: Jim Xia <jim...@hotmail.com>
Date: Fri, 9 May 2008 10:32:53 -0700 (PDT)
Local: Fri, May 9 2008 1:32 pm
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?

> 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.

Cheers,

Jim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Giles  
View profile
 More options May 9, 2:20 pm
Newsgroups: comp.lang.fortran
From: "James Giles" <jamesgi...@worldnet.att.net>
Date: Fri, 09 May 2008 18:20:57 GMT
Local: Fri, May 9 2008 2:20 pm
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
glen herrmannsfeldt  
View profile
 More options May 9, 4:22 pm
Newsgroups: comp.lang.fortran
From: glen herrmannsfeldt <g...@ugcs.caltech.edu>
Date: Fri, 09 May 2008 12:22:49 -0800
Local: Fri, May 9 2008 4:22 pm
Subject: Re: Can "Final subroutines" in F2003 delete pointer components in a class?

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.

-- glen


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »