- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.....
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
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 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..
[bash]
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
[/bash]
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
[bash]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[/bash]
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
[bash]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 [/bash]
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..
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there are particular reason you are using pointers? If you can use allocatable components and variables, then things are generally safer. Pointers are required for things like circular lists, or where you want to reliably point at components of the list from client code.
If you do need to use pointers, consider default initialisation of the pointer components to NULL - saves on a few lines of code and prevents problems if you forget to nullify components of a newly allocated entity.
Could you perhaps present a small compilable and runnable driver program that used your list? If you can, you can use the "add files" button to present it to the forum and avoid the problems you saw with the syntax highlighter.
If you do need to use pointers, consider default initialisation of the pointer components to NULL - saves on a few lines of code and prevents problems if you forget to nullify components of a newly allocated entity.
Could you perhaps present a small compilable and runnable driver program that used your list? If you can, you can use the "add files" button to present it to the forum and avoid the problems you saw with the syntax highlighter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I use pointer because I don't know how big exactly the list will be and if I should add one new element to the list, as well as how many elements I will add in specific subroutine, these information all depends.
I'm not sure if allocatable variables can fulfill this,won't they be released when exit the subroutine allocate them?
I'm not sure if allocatable variables can fulfill this,won't they be released when exit the subroutine allocate them?
And, as you can see I set all pointers to NULL for initialization before do allocate.
PS: Another reason I use pointers to realize a list instead of a allocatable array because the list may be very big, and I need its value is in ordered from small to big, I think using pointers can void large computation cost on sorting.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page