Re: Debug my program please.
- From: "Jugoslav Dujic" <jdujic@xxxxxxxxx>
- Date: Fri, 28 Oct 2005 09:04:08 +0200
Andy Spragg wrote:
| On Thu, 27 Oct 2005 17:50:52 +0200, "Jugoslav Dujic"
| <jdujic@xxxxxxxxx> wrote:
|
|| Richard E Maine wrote:
|
||| expressions with multiple function references. For a function that
||| returns a pointer, you are almost always going to reference it like
||| p => the_function(whatever)
||| in which case, you could jut as well have written
||| call a_subroutine(p, whatever)
||| Both do work, but I just nee no advantage to the function form that
||| outweighs the number of errors I see.
|
|| OK, I understand; gotta run now, just to mention that there's another
|| useful context where function is more elegant than a subroutine:
||
|| call SomethingUnrelated(theFunction(whatever))
||
|| Where dummy argument of SomethingUnrelated may but need not be
|| a pointer; if whatever is a large object, that could easily
|| spare you copying data around, and certainly saves you a
|| temporary pointer.
|
| OK, I definitely /don't/ understand! I've only ever used pointers once
| or twice for autodidactic purposes (to try and find out why I would
| ever want to use one in anger; haven't convinced myself yet), and I've
| certainly never yet found a need to use either a subroutine or a
| function to return one; but I've seen Richard's caveat often enough to
| know that should I ever need to do so, his point 3 pretty well sums up
| all I need to know.
|
| And then along you come and convince me that it doesn't. Except that I
| haven't the faintest idea what you mean. Could you possibly spell it
| out a little more explicitly for me?
Please see the context -- I was referring to the case when you
return a pointer to an existing target. I still agree that returning
a pointer to freshly allocated memory is asking for trouble.
The pseudo code I shown is referring to some pieces of my Xeffort
library. I'll explain the usage on that example (albeit it's
about GUI stuff, so a bit unusual for Fortran) because I think
it's plastic enough; however, I think it's a common design
pattern otherwise.
The library maintains a pool of objects (in this case, windows)
where every object is modeled as a TYPE. There's no single place
of creation of objects, so they can't be referred to by e.g. an
array or its index. Instead, they can be referred to by a
hash table identifier:type. Thus, the library has a pool
(array, linked list or whatever) of objects, but in general it's
not (and should not be) accessible from user level code as such.
When the user code needs to get an object (TYPE), it
can call a library routine to get a POINTER to an existing
object, in my case:
FUNCTION XGetWindow(hWnd) RESULT(xWnd)
TYPE(X_WINDOW), POINTER:: xWnd
INTEGER, INTENT(IN):: hWnd
hWnd is "identifier" (aka handle) of the object (window)
and the function returns a pointer to an existing object
from the pool.
Now, having it in function form rather than subroutine
is more practical than subroutine. If it were a subroutine,
you will have to declare a local pointer just for temporary
passing, like:
type(X_WINDOW), pointer:: xTemp
....
call XGetWindow(hWnd, xTemp)
call XShowWindow(xTemp)
instead, you can write an one-liner ((C) David Frank):
call XShowWindow(XGetWindow(hWnd,xTemp))
Further, using the pointers all around is far more efficient
than non-pointers; in the latter case, you would have a
lots of copying of X_WINDOWs. In this case, storage for
the objects is allocated once (somewhere) and you only pass
pointers to it all around, which is cheap.
--
Jugoslav
___________
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
.
- References:
- Debug my program please.
- From: TC
- Re: Debug my program please.
- From: Richard E Maine
- Re: Debug my program please.
- From: Richard E Maine
- Re: Debug my program please.
- From: Jugoslav Dujic
- Re: Debug my program please.
- From: Andy Spragg
- Debug my program please.
- Prev by Date: memory not freed up?
- Next by Date: Re: Fortran Compling problem
- Previous by thread: Re: Debug my program please.
- Next by thread: Re: Debug my program please.
- Index(es):
Relevant Pages
|