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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

restrict dimensions for allocatable arrays

Status
Not open for further replies.

EvgeniyZh

Programmer
May 30, 2009
2
DE
Hello,
I'm just writing some code in Fortran 90 and compiling it with ifort. For some data I need arrays of size e.g. 3 x n, where n is determinded at runtime. AFAIK fortran does not allow to restrict any dimension of my allocatable array, so in fact the arrays intended to be 3 x n could be allocated as m x n, where m could be random. I thought of defining a datatype for a 3-dimensional vector, but that would slow down the intrinsic vecotr operations and I would have to rewrite some standart operations by hand (e.g. + or *). Additionally some overhead would be produced because I would use a custom type. Is there any possibility to restrict one of the dimensions for an allocatable array, without running into trouble described above? (I would need something like
real, allocatable :: A(3,:)).
 
If you mean restrict it in the declaration, no. Best bet is to check before allocation.
 
Thanks a lot! Yes, I meant in the declaration. It's a pity...
 
You could always do something like
Code:
subroutine malloc(A, nsize)
real, allocatable, intent(inout)::A(3,:)
integer, intent(in):: nsize
integer, parameter:: maxallowed = 1000

if (nsize .gt. maxallowed) then
   print *, 'Array size ', nsize, ' exceeds max ', maxallowed
   stop
end if
allocate (A(3:nsize))
return
end
Then call malloc instedad of allocate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top