Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran Gurus, Please comment!! 1

Status
Not open for further replies.

kapahianil

Programmer
Apr 29, 2010
3
US
Please have a look at piece of code below and try to answer the questions given in comments. Thanks.
Anil

Program TEST
IMPLICIT NONE

INTEGER :: nch, iside, i

! cell pointer type
TYPE :: cell_ptr
TYPE(cell_type), POINTER :: cptr
END TYPE cell_ptr

TYPE :: cell_type
! cell index
INTEGER, DIMENSION(1) :: index
! Pointers to neighbors
TYPE(cell_ptr), DIMENSION(1:27) :: nbrcell !! As cell_ptr type is a
!! pointer does that mean nbrcell is a pointer too.
END TYPE cell_type

TYPE(cell_ptr),DIMENSION:)), POINTER :: basecell
TYPE(cell_type), TARGET :: cell0

! Initialize cell0
cell0%index(1) = 0

DO iside = 1, 27
cell0%nbrcell(iside)%cptr => cell0
ENDDO


ALLOCATE(basecell(0:10000))

DO i = 0,10000 !! Do I need This
ALLOCATE(basecell(i)%cptr) !! part?Please give your comments as the
END DO !! code works the same with and without this part

basecell(0)%cptr => cell0

DO iside = 1,27
PRINT*,"cell0=",cell0%nbrcell(iside)%cptr%index(1),"iside=",iside
PRINT*,"basecell0=",basecell(0)%cptr%nbrcell(iside)%cptr%index(1),"iside=",iside
END DO

END Program TEST
 
Yes you do. You will need it for anything that accesses %cptr%...
 
Hi xwb!!
I understand that I need it for anything that accesses %cptr%. But Then how come this thing still prints PRINT*,"basecell(0)%cptr%nbrcell(iside)%cptr%index(1),"iside=",iside
even if I don't allocate "ALLOCATE(basecell(i)%cptr)".
Thanks
Anil
 
I didn't see

basecell(0)%cptr => cell0

That clobbers all your allocations. That's why you don't need it. You also get a memory leak.
 
Hi,
I am still confused now I have added u to cell type. And when I allocate memory for that I get error that
forrtl: severe (174): SIGSEGV, segmentation fault occurred.
But If I do Allocate(basecell(0:1000)%cptr), there is no error and things work fine.
Thanks
Anil


Program TEST

IMPLICIT NONE

INTEGER :: iside,i

! cell pointer type (LMR)

TYPE :: cell_ptr
TYPE(cell_type), POINTER :: cptr
END TYPE cell_ptr



TYPE :: cell_type
! Location parameters and indices

! cell index
INTEGER, DIMENSION(2) :: index

! Velocity components
DOUBLE PRECISION, POINTER :: u

! Pointers to neighbors
TYPE(cell_ptr), DIMENSION(1:27) :: nbrcell

END TYPE cell_type

TYPE(cell_ptr),DIMENSION:)), POINTER :: basecell

TYPE(cell_type), TARGET :: cell0

iside =0
i = 0


! Initialize cell0
cell0%index(1:2) = 0


DO iside = 1, 27
cell0%nbrcell(iside)%cptr => cell0
ENDDO


ALLOCATE(basecell(0:10000))

DO i = 0,10000
ALLOCATE(basecell(i)%cptr%u)
END DO

basecell(0)%cptr => cell0

DO iside = 1,27
PRINT*,"cell0=",cell0%nbrcell(iside)%cptr%index(1),"iside=",iside
PRINT*,"basecell0=",basecell(0)%cptr%nbrcell(iside)%cptr%index(1),"iside=",iside
END DO

END Program TEST
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top