For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2004 > Allocatable components









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Allocatable components
B52B

2004-07-13, 9:01 am

Hi,

Is it possible to use pointers in the following way?

program test
implicit none

type :: T
real, allocatable :: a(:)
end type T

type(T), pointer :: T_ptr => null()

allocate (T_ptr%a(10))

T_ptr%a = 5

print *, T_ptr%a(1)

deallocate (T_ptr%a)

end program test

This is an analogy with the following code:

real, dimension(:), pointer :: x_ptr

allocate (x_ptr(10)

Regards,
B52B
Arjen Markus

2004-07-13, 9:01 am

B52B wrote:
>
> Hi,
>
> Is it possible to use pointers in the following way?
>
> program test
> implicit none
>
> type :: T
> real, allocatable :: a(:)
> end type T
>
> type(T), pointer :: T_ptr => null()
>
> allocate (T_ptr%a(10))
>
> T_ptr%a = 5
>
> print *, T_ptr%a(1)
>
> deallocate (T_ptr%a)
>
> end program test
>
> This is an analogy with the following code:
>
> real, dimension(:), pointer :: x_ptr
>
> allocate (x_ptr(10)
>
> Regards,
> B52B



Allocatable components in derived types are not conforming to the
Fortran 90 or 95 standard. Some compilers do support them (there
is a "TR" for this), but your best bet is to use code like:
program test
implicit none

type :: T
real, pointer :: a(:) !<=== note the difference
end type T

type(T), pointer :: T_ptr => null()

allocate (T_ptr) !<=== you forgot that one
allocate (T_ptr%a(10))

T_ptr%a = 5

print *, T_ptr%a(1)

deallocate (T_ptr%a)
deallocate (T_ptr) ! Just to be sure
end program test


Regards,

Arjen
Richard Maine

2004-07-13, 4:00 pm

B52B <B52B@pl.pl.pl> writes:

> Is it possible to use pointers in the following way?


No. Arjen commented about the allocatable components.
I'll ignore that; it is fine in f95+TR or in f2003.
You have another problem that is more fundamental and
is not ok in any version.

> program test
> implicit none
>
> type :: T
> real, allocatable :: a(:)
> end type T
>
> type(T), pointer :: T_ptr => null()
>
> allocate (T_ptr%a(10))


No. The problem with the last line above is that
T_ptr is null, so you can't write T_ptr%anything.

You are confusing the allocation of T_ptr from the allocation of its
component. They are separate things. Let me repeat that because it
is fundamental and I don't want you to read past it too fast - they
are separate things. If you wanted the above to work, you'd have to
do something like

allocate(t_ptr)

prior to doing the allocate of T_ptr%a. But then, if this was
all you were going to do, it would be a bit silly as there is
no reason for the pointerness of T_ptr. You might just as

well have declated T_ptr as

type(T) :: T_ptr

In fact, with just that change, the rest of your code should work
(assuming you are using a compiler that supports allocatable
components and assuming I didn't overlook something).

The only reason I could see for making T-ptr a pointer is if you
wanted to do pointer-related stuff with T_ptr *ITSELF* - not just
with its components. The above code certainly doesn't do that.
I suppose this sample might have just omitted that stuff, but
I'm suspicious that the pointer attribute is just superfluous
here and is the source of your problem.

> This is an analogy with the following code:
>
> real, dimension(:), pointer :: x_ptr
>
> allocate (x_ptr(10)


Ah. But there are 2 differences. One difference is in allocatable
versus pointer. This is somewhat peripheral to the problem with the
above code, but it does make me wonder what you are trying to achieve.
Are you doing things that are fundamentally pointer-related or just
using pointers as a substitute for allocatables? If you want to
change the pointer to allocatable, all you need to do is literally
that, as in

real, dimension(:), allocatable :: x_ptr

There is no need to introduce a derived type for this. But
perhaps you need the derived type for other purposes (such as
making an array of these things), so I'll ddrop that question as
it really is peropheral.

But the difference that *DOES* affect your code is that your
sample program has *TWO* things that need allocation - the
pointer and the allocatable component. As I said earlier
(and I'll repeat once again just to make sure), they are
separate things. A single allocate does not allocate both
of them.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
B52B

2004-07-14, 8:58 am

Arjen, Richard thank you for your comments.

Regards,
B52B


B52B wrote:
> Hi,
>
> Is it possible to use pointers in the following way?
>
> program test
> implicit none
>
> type :: T
> real, allocatable :: a(:)
> end type T
>
> type(T), pointer :: T_ptr => null()
>
> allocate (T_ptr%a(10))
>
> T_ptr%a = 5
>
> print *, T_ptr%a(1)
>
> deallocate (T_ptr%a)
>
> end program test
>
> This is an analogy with the following code:
>
> real, dimension(:), pointer :: x_ptr
>
> allocate (x_ptr(10)
>
> Regards,
> B52B

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com