View previous topic :: View next topic |
Author |
Message |
MattMcD
Joined: 06 Sep 2007 Posts: 19 Location: Cambridge,UK
|
Posted: Tue Apr 01, 2008 7:07 pm Post subject: allocateable array of integers / Ref through Null Pointer |
|
|
I'm having an issue with the latest ftn95 5.2.1 which goes along the lines of "Error: Reference through NULL Fortran POINTER"
Module Mod_Name
integer,dimension(,allocatable,save :: array1
Contains
Subroutine Sub_Name
integer :: k,i,stat
k = 0
do i = 1,mxI
if (condition(i)) k = k +1
end do
allocate (array1(k),STAT = stat)
do i = 1,k
array1(i) = some value !!!!!!!!!!!!
end do
...something else...
End Subroutine Sub_Name
End Module Mod_Name
Which errors at the !!!!!! marked line
in the debugger right clicking on array1 yields "no pointer"
the stat value for on allocation is 2 (but I can't find a table to tell me what these stat values mean and I'm sure non-zero means it didn't work!)
Thanks! |
|
Back to top |
|
|
brucebowler Guest
|
Posted: Tue Apr 01, 2008 7:52 pm Post subject: Re: allocateable array of integers / Ref through Null Pointe |
|
|
MattMcD wrote: | do i = 1,mxI
if (condition(i)) k = k +1
end do
allocate (array1(k),STAT = stat)
do i = 1,k
array1(i) = some value !!!!!!!!!!!!
end do
|
What's the value of mxI and what does condition look like? In other words, is k getting incremented as expected?[/code] |
|
Back to top |
|
|
MattMcD
Joined: 06 Sep 2007 Posts: 19 Location: Cambridge,UK
|
Posted: Wed Apr 02, 2008 9:29 am Post subject: |
|
|
well it turns out that k = 8 in this case incremented as expected,
I hope that an array of 8 integers is less than the heap size!
the code I posted was an over simplification, its in a read file routine so its actually:
do
read(unit=rdunit,fmt='(1x,i4)',iostat = stat) iin1
if (iin1 == 0) exit
if (InputGroup(iin1,inpgrp1) then
k = k + 1
else
exit with error message
end if
end do
allocate (array1(k),STAT = stat)
do i = 1,k
array1(i) = some value !!!!!!!!!!!!
end do
InputGroup is a 2D logical array that runs over all inputs and input groups and is true if iin1 is in the group referenced by the second variable
with the debugger the program functions as expected untill the allocate array1 statement is executed |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Wed Apr 02, 2008 9:47 am Post subject: |
|
|
Can you post some working code that fails in this way, that is code that compiles and fails with the NULL prointer.
Alternatively I can give you an email address to send the code to. |
|
Back to top |
|
|
MattMcD
Joined: 06 Sep 2007 Posts: 19 Location: Cambridge,UK
|
Posted: Wed Apr 02, 2008 9:54 am Post subject: |
|
|
can I get an email address, its not in a simple program at all!
Don't think the boss would be very happy with me posting big chunks of real code up! |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Wed Apr 02, 2008 9:56 am Post subject: |
|
|
You could test if K<=0 or array1 is already allocated before the allocate statement. Report the value of K before allocating.
regards John |
|
Back to top |
|
|
MattMcD
Joined: 06 Sep 2007 Posts: 19 Location: Cambridge,UK
|
Posted: Wed Apr 02, 2008 10:57 am Post subject: |
|
|
John,
use of
if (.not.allocated(array1))
allocate(array1(k))
else
deallocate(array1)
allocate(array1(k))
end if
was tested and didn't help
I haven't put in explicit check on k in the code, but have using the debugger verified that the value of k before execution is the integer 8 |
|
Back to top |
|
|
MattMcD
Joined: 06 Sep 2007 Posts: 19 Location: Cambridge,UK
|
Posted: Wed Apr 02, 2008 11:02 am Post subject: |
|
|
using the vars table before execution of the allocate statement reports that
array1 = integer * 4 (-2139062144:16843008)
which as far as my knowledge goes indicates that it is an array the size of the heap of 4 byte integers and I thought that allocate would then change the bounds to those specifies and reduce the size of the heap by the requisite amount... |
|
Back to top |
|
|
MattMcD
Joined: 06 Sep 2007 Posts: 19 Location: Cambridge,UK
|
Posted: Tue Apr 08, 2008 12:32 pm Post subject: |
|
|
Using Nullify(array1) instead of deallocate(array1) seemed to do the trick
If allocatable and pointer are synonymous then should deallocate be synonymous (or incorporate) nullify?
No idea why it happened in this one particular instance! |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Tue Apr 08, 2008 12:58 pm Post subject: |
|
|
I have not followed the detail of this conversation but you will probably need to nullify if there is a call to deallocate before the first call to allocate. |
|
Back to top |
|
|
|