|
|
Up |
|
|
  |
Author: Mark StuckyMark Stucky Date: Jul 31, 2008 16:42
First of all, sorry about the length of this post. I tried
to come up with a short piece of example code, but between the
code and problem description, it's still about 350+ lines.
I have been trying to improve my skills with regard to "f90"
syntax, but I've hit a snag. I'm fairly certain that I'm still
missing something with regard to understanding POINTERs, although
I've been programming long enough to realize that my problem
could be un-related and some silly mistake in some other section
of code.
What I'm trying to do is to create an array of a derived type
(refer to the sample code below for the actual implementation)
type (typ_surface),allocatable :: surf(:)
type (typ_surface),pointer :: surfptr
nsurf = 3
allocate(surf(nsurf))
where the derived types are declared as
|
| Show full article (10.29Kb) |
|
| | 13 Comments |
|
  |
Author: nospamnospam Date: Jul 31, 2008 17:32
Mark Stucky cox.net> wrote:
> I'm fairly certain that I'm still
> missing something with regard to understanding POINTERs,...
Likely. See below.
> call alloc_surf(surfptr,ni,nj,status)
> surf(n) = surfptr
It isn't directly related tp your question, but the above strikes me as
oddly indirect and complicated. I'm not sure why you don't just directly
initialize surf(n) instead of allocating a temporary surfptr and then
copying its target to surf(n). That is just do
call alloc_surf(surf(n),ni,nj,status)
and, of course, get rid of the pointer attribute and the corresponding
allocation/deallocation in alloc_surf. Perhaps there is some reason that
didn't show through in my quick glance, or even was elided from the
posted code.
Anyway, on to the more central part.
|
| Show full article (3.10Kb) |
|
| | no comments |
|
  |
Author: dpbdpb Date: Jul 31, 2008 17:39
Mark Stucky wrote:
...
> If it matters, the compiler that I'm using is Compact Visual
> Fortran, version 6.5 ...
...
I don't know if the update from V6.5 to V6.6 made in fixes that are in
areas that might have anything whatsoever to do with your problem; in
that regard I'd suggest fixing the problem Richard pointed out first
then go from there--if it is the only issue (and I read even less after
noting Richard had made his comment) you're on your way; if not either
you'll be able to see a problem yourself or can repost w/ a new issue.
But, the update to 6.6 is available at the HP Fortran site -- I don't
have a direct url, but a search thru the HP Fortran related stuff does
return it. Might be worth updating to at least the latest of what there
is. I'll note I'm also one stuck in the time warp of CVF but haven't
enough code which uses pointers that I still support to have any
experience as to what does or might not work w/ CVF. So, it's just a
general suggestion that may have no particular relevance...
--
|
| |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Jul 31, 2008 18:43
Mark Stucky wrote:
(snip)
> if (associated(a)) then
> deallocate(a)
> endif
I am suspicious of the above statements (in two places).
Running with g95, it fails in the deallocate statement above.
Commenting out both of those, it runs and gives the desired
result. That is, both "correct" and "incorrect" output
are the same.
Deallocating memory that hasn't been allocated does very
strange things to the memory that is allocated.
-- glen
|
| |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Jul 31, 2008 18:44
Mark Stucky wrote:
(snip)
> if (associated(a)) then
> deallocate(a)
> endif
I am suspicious of the above statements (in two places).
Running with g95, it fails in the deallocate statement above.
Commenting out both of those, it runs and gives the desired
result. That is, both "correct" and "incorrect" output
are the same.
Deallocating memory that hasn't been allocated does very
strange things to the memory that is allocated.
-- glen
|
| |
| no comments |
|
  |
Author: Rich TownsendRich Townsend Date: Jul 31, 2008 18:27
Richard Maine wrote:
> Mark Stucky cox.net> wrote:
>
>> I'm fairly certain that I'm still
>> missing something with regard to understanding POINTERs,...
>
> Likely. See below.
>
>> call alloc_surf(surfptr,ni,nj,status)
>> surf(n) = surfptr
>
> It isn't directly related tp your question, but the above strikes me as
> oddly indirect and complicated. I'm not sure why you don't just directly
> initialize surf(n) instead of allocating a temporary surfptr and then
> copying its target to surf(n). That is just do
>
> call alloc_surf(surf(n),ni,nj,status)
>
> and, of course, get rid of the pointer attribute and the corresponding
> allocation/deallocation in alloc_surf. Perhaps there is some reason that ...
|
| Show full article (3.28Kb) |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Jul 31, 2008 23:19
Rich Townsend wrote:
(snip)
>>
>>> if (associated(surf)) then
>>> deallocate(surf)
>>> endif
(snip)
> To expand to what Richard said, you can initialize pointer components
> like this:
> type(typ_2D_array),pointer :: x => null()
To expand even more, there is no need for the test at all.
With ALLOCATABLE variables, there is sometimes need to test
and deallocate before reallocating. That might sometimes be
true in the case of pointers, but not in this case. The program
wants to allocate a new pointer each time. There is no need
to check the pointer variable at all. No need to assign null()
to the pointer, and then check its allocation status in the
following statement. There is no rule against allocating using
a pointer variable that already happens to point to something.
|
| Show full article (0.93Kb) |
| no comments |
|
  |
Author: Rich TownsendRich Townsend Date: Aug 1, 2008 08:09
glen herrmannsfeldt wrote:
> Rich Townsend wrote:
> (snip)
>
>>>
>>>> if (associated(surf)) then
>>>> deallocate(surf)
>>>> endif
> (snip)
>
>> To expand to what Richard said, you can initialize pointer components
>> like this:
>
>> type(typ_2D_array),pointer :: x => null()
>
> To expand even more, there is no need for the test at all.
>
> With ALLOCATABLE variables, there is sometimes need to test
> and deallocate before reallocating. That might sometimes be
> true in the case of pointers, but not in this case. The program ...
|
| Show full article (1.20Kb) |
| no comments |
|
  |
Author: Tom MicevskiTom Micevski Date: Aug 1, 2008 09:46
dpb wrote:
> Mark Stucky wrote:
> ....
>> If it matters, the compiler that I'm using is Compact Visual
>> Fortran, version 6.5 ...
> ....
...
> But, the update to 6.6 is available at the HP Fortran site -- I don't
> have a direct url, but a search thru the HP Fortran related stuff does
> return it.
|
| Show full article (0.87Kb) |
| no comments |
|
  |
|
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Aug 1, 2008 11:47
Rich Townsend wrote:
(snip)
>> There is no rule against allocating using
>> a pointer variable that already happens to point to something.
> ...unless you are concerned about memory leaks?
Well, you should copy the pointer somewhere else before
the new ALLOCATE. For the OPs problem, a new pointer should
be allocated each time, whether or not the variable happens to
be pointing to a previously allocated structure.
-- glen
|
| |
| no comments |
|
RELATED THREADS |
  |
|
|
|
|
|