View previous topic :: View next topic |
Author |
Message |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Thu Jan 23, 2014 5:10 pm Post subject: Allocated array error-Reason? |
|
|
Hello,
Please can anyone help me here. I can't get this working but really need to...
I have delclared an allocatable array in a module as below.
Code: | MODULE MyModule
double precision, ALLOCATABLE :: My_Allocated_Array(:)
END MODULE MyModule |
I have a subroutine defined as
Code: | subroutine MySubRoutineA
USE MyModule
ALLOCATE My_Allocated_Array(15)
..... //I do something with My_Allocated_Array
call MySubRoutineB (
(My_Allocated_Array) |
I know that I do not need to pass necessarily pass My_Allocated_array to MySubRoutineB . Let us say I pass
Now,
Code: | MySubRoutineB (My_Array_New_Name)
dimension My_Array_New_Name(*)
.............. |
I get an error on compilation stating that
Quote: | Error 1 Error: The type of the actual argument differs from the type of the dummy argument. [My_Allocated_Array] |
Can anyone please help? I have an old fortran code edited and I need this to work.
Is there soemthing fundamentally wrong?
Please help if possible, gratefully appreciated
Christy |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Fri Jan 24, 2014 5:00 am Post subject: |
|
|
Please can John or anyone advise? I shall be grateful. |
|
Back to top |
|
 |
jalih
Joined: 30 Jul 2012 Posts: 196
|
Posted: Fri Jan 24, 2014 6:52 am Post subject: |
|
|
Try using assumed-shape array, something like:
Code: |
program test
integer, allocatable :: a(:)
allocate(a(5));
a = (/1,2,3,4,5/);
call test2(a)
contains
subroutine test2(array)
integer :: array(:)
write(*,*) array
end subroutine test2
end program test
|
|
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Fri Jan 24, 2014 8:42 am Post subject: |
|
|
change:
MySubRoutineB (My_Array_New_Name)
dimension My_Array_New_Name(*)
to:
MySubRoutineB (My_Array_New_Name)
double precision My_Array_New_Name(*) |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Wed Jan 29, 2014 8:46 am Post subject: |
|
|
@John- Tried but didn't work.
@jalih- it does not give error but gives a warnign stating:
Warning: Required interface for passing assumed shape array is missing from original source
Can the warning be neglected? |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Wed Jan 29, 2014 9:45 am Post subject: |
|
|
You could simplify the declarations and not use (:) syntax, which requires an interface definition, (unless it is a requirement ?)
The following example does this and you should be able to adapt to your problem
Code: | MODULE MyModule
double precision, ALLOCATABLE :: My_Allocated_Array(:)
END MODULE MyModule
subroutine MySubRoutineA
USE MyModule
ALLOCATE ( My_Allocated_Array(15) )
!..... //I do something with My_Allocated_Array
call MySubRoutineB (My_Allocated_Array, size(My_Allocated_Array) )
...
MySubRoutineB (My_Array_New_Name, n)
! transfer the dimension rather than use an interface for the dimension
integer n
double precision My_Array_New_Name(n)
..............
|
or
Code: | MODULE MyModule
real*8, ALLOCATABLE :: My_Allocated_Array(:)
END MODULE MyModule
subroutine MySubRoutineA
USE MyModule
ALLOCATE ( My_Allocated_Array(15) )
!..... //I do something with My_Allocated_Array
call MySubRoutineB (My_Allocated_Array, 15) ! size(My_Allocated_Array) = 15
...
subroutine MySubRoutineB (My_Array_New_Name, n)
! transfer the dimension rather than use an interface for the dimension
integer n
real*8 My_Array_New_Name(n)
|
|
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Wed Jan 29, 2014 2:12 pm Post subject: |
|
|
Thanks John,
1) When I use ALLOCATABLE arrays, it permits me to use very huge array size.
Doing that (as in your code snippet), will it permit me to use very huge arrays (of size say 3 million or even more?)?
2) If I do not pass the size of the array - is there any other option available?
Thanks again |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Wed Jan 29, 2014 10:56 pm Post subject: |
|
|
1) You should use an error test on allocate, as:
ALLOCATE ( My_Allocated_Array(15), stat=istat )
if ( ISTAT /= 0) then ! respond to error
arrays of 3 million should not be a problem, as you could have up to 1,500 million bytes available.
2) If the subroutine MySubRoutineB needs to know the size of the array, then it must be provided. You can use an interface block or more simply provide it as an argument, as I have shown.
Alterntively you could not provide the array size, as:
real*8 My_Array_New_Name(*)
and then use it. You can write the code ensuring you don't overflow the array size. The compiler will not be able to check this.
John |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Thu Jan 30, 2014 7:30 am Post subject: |
|
|
Thanks John,
This is an existing code I'm modifying.
I do not intend using an argument as you provided (if possible)
Now, you say about an interface block.
Already there is a definition as below (elsewhere)
Code: | MODULE MyModule
double precision, ALLOCATABLE :: My_Allocated_Array(:)
END MODULE MyModule |
Where should the interface block be provided i.e. I'm actually repeating the same definition, right? |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Fri Jan 31, 2014 12:59 am Post subject: |
|
|
I think you would be better off to "USE MyModule" in MySubRoutineB and not change the name of the array.
Code: | MODULE MyModule
real*8, ALLOCATABLE :: My_Allocated_Array(:)
END MODULE MyModule
subroutine MySubRoutineA
USE MyModule
ALLOCATE ( My_Allocated_Array(15) )
!..... //I do something with My_Allocated_Array
call MySubRoutineB (other_arguments)
...
subroutine MySubRoutineB (other_arguments)
USE MyModule, My_Array_New_Name => My_Allocated_Array
integer other arguments
...
|
You can use (something like?) the example above to link the local name to the variable name in the module but I would not recommend this approach. Keep it simple!
John |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Fri Jan 31, 2014 9:20 am Post subject: |
|
|
Thanks John
Did you mean the statement,
My_Array_New_Name => My_Allocated_Array
alllows to use the array name as instead of original Quote: | My_Allocated_Array | that was declared in MODULE? |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Sun Feb 02, 2014 11:59 pm Post subject: |
|
|
christyleomin,
Yes that is what I meant.
A good document to use is the Lahey Language Reference .pdf, as it has a good alphabetical list of most Fortran structures.
lahey.com > Support > Documentation > Express Language Reference
John |
|
Back to top |
|
 |
|