I'm using fortran to realize a list, which contains a lot of pointers needed to be allocate in one subroutine and then deallocate in another subroutine. It cost meone week to find out my way using pointers is unsafe );, causing compiler say "A pointer passed to DEALLOCATE points to an array that cannot be deaallocated", sometimeseven say "Subscript #2 of the array TYP has value 0 which is greater than the upper bound of -35783205". Howcan Iusing pointer safely between subroutines and safely deallocate them? good guys please tell me, thank you.....
TYPE :: listele !the base type of our list INTEGER, POINTER, dimension ( : ) :: x INTEGER :: d END TYPE listele TYPE :: p2listele !to realize a point array CLASS (listele), POINTER :: p END TYPE p2listele TYPE, EXTENDS(listele) :: mdslist !the final list TYPE (p2listele), POINTER, dimension ( :, : ) :: typ END TYPE mdslist
this is cotained in a MODULE....saying "list". Then I use it in another module whichcontains subroutineallocate some type(mdslist) pointers, and some other subroutine deallocate them. something like
SUBROUTINE produce() USE list TYPE (mdslist), POINTER :: tp INTEGER :: v,w tp=>NULL() ALLOCATE(tp) tp%d=3 tp%x=>NULL() tp%typ=>NULL() ALLOCATE(tp%x(1:tp%d)) ALLOCATE(tp%typ(1:2,0:1)) forall (v=1:2, w=0:1) tp%typ(v,w)%p=>NULL() end forall call inser(head, tp) end SUBROUTINE produce
where head is class(lisele) pointer, declared in anothermodule, so I can somehowglobally using "head", inser is a method, which connect two type(mdslist) with the %typ(:,:)%p pointer. for example,set "head0"
to null, and call inser(head0, a) and then inser(head0,b), I should get a list has a head named "head0" with
a%typ(2,1)%p=>b, b%typ(2,0)%p=>a, associated(a, head0)==.true.
Here comes the problem
when I deallocate a member in the list in other subroutines, something like
SUBROUTINE delete(head, np) call loc(head, lcp, np, k) tp1=>lcp%(2,0)%p tp2=>lcp%(2,1)%p tp1%typ(2,1)%p=>tp2 tp2%typ(2,0)%p=>tp1 deallocate (lcp) end SUBROUTINE delete
subroutine loc tellsme "np" get to be deletedwhere is inthe list and give its position by point lcp to np, the compiler always tells "A pointer passed to DEALLOCATE points to an array that cannot be deallocated", I assume it is because "np" is a argument in delete or maybe compiler cannot tell np is a single element or array. However, I can't think out any better solution to using the list. And, sometimes, not always, as I mentioned, in runtime, the program may say "Subscript #2 of the array TYP has value 0 which is greater than the upper bound of -35783205", I'm absolute sure typ has been allocated at this time, and according to my allocation there is always 0 in subscript #2, I have no idea why.
Maybe there is a totally better way to using a list like this, or even another way to design the list, I really can't think out now....
PS: all subroutines are in some module or declared in interfaces
well, the gt; in codes is actually ">", I have no idea how to get rid of it, wish don't bother you..