Re: Debug my program please.



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



Relevant Pages

  • Re: Procedure Pointer (Components) with no explicit interface and with implicit typing
    ... in principle, I can assign to it either a function nor a subroutine, e.g. ... "7.4.2.2 Procedure pointer assignment" ... "If proc-pointer-object has an implicit interface and is explicitly ... external:: func, sub ...
    (comp.lang.fortran)
  • Re: Pointer-valued function to access inner components
    ... implicit none ... Return a F90 pointer which points to the diagonal of T. ... a copyin/copyout on array A during the procedure call. ... subroutine dirty_pointer_1d ...
    (comp.lang.fortran)
  • Re: Fortran based MEX w/ COMMON/SAVE
    ... I believe dat (pointer to the array) and the ... SAVE at the begining of each subroutine is a bit unusual to me. ... > to unload it if there isn't a Fortran END or STOP statement. ... interaction w/ Matlab). ...
    (comp.soft-sys.matlab)
  • background process to monitor cursor location
    ... Inside the Document_Opensubroutine, I am using the Timer and DoEvents ... text form fields used for data entry, then a pointer to that field is stored. ... My concern/question: ...
    (microsoft.public.word.vba.general)
  • Re: Copy-in/Copy-out
    ... -|declared as a target or pointer, it should be safe to assign a pointer ... -| Character, Intent:: str* ... -| End Interface ... -| Subroutine SubTarget! ...
    (comp.lang.fortran)