replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Array bound checking
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Array bound checking

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
simon



Joined: 05 Jul 2006
Posts: 299

PostPosted: Tue Aug 24, 2010 5:36 pm    Post subject: Array bound checking Reply with quote

The following code allocates an array with one dimension size 0, but does not give an array bounds error message when compiled with /CHECK. It's not obvious to me whether ALLOCATE should have returned non-zero, but it does not.

Code:

PROGRAM p1
  INTEGER :: n=0,m=5
  INTEGER :: i,ifail
  INTEGER, ALLOCATABLE :: a(:,:)
  INTRINSIC SHAPE,SIZE
!
  ALLOCATE (a(n,m),STAT=ifail)
  IF (ifail/=0) STOP
  PRINT *, SIZE(a)
  PRINT*, SHAPE(a)
  DO i=1,m
     a(:,i)=i
  END DO
  PRINT *, a
END PROGRAM p1
Back to top
View user's profile Send private message
Sebastian



Joined: 20 Feb 2008
Posts: 177

PostPosted: Tue Aug 24, 2010 7:43 pm    Post subject: Reply with quote

http://forums.silverfrost.com/viewtopic.php?t=1235
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Tue Aug 24, 2010 9:34 pm    Post subject: Reply with quote

This is a very interesting program.

The Fortran standard does not appear to say anything about whether ALLOCATE should fail for a zero size array so the compiler writer has a choice.

Try compiling the program with /explist and look at the resulting .lis file.

a(:,i) = i is expanded as a do loop that is not entered. Hence it does not fail with the /check mechanism.

As in the suggested link, the problem will become apparent when you try to access elements of the array.
Back to top
View user's profile Send private message AIM Address
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Wed Aug 25, 2010 4:44 am    Post subject: Reply with quote

Paul,

My understanding is that the Standard does specifically address the size of zero. ( I don't have a copy of the Standard!!) I recall many years ago Tom Lahey explaining this aspect of arrays in Fortran 90/95.
The array can be defined, and used as in an argument list of subroutines.
You can use it in array expressions, but can not use it when referencing elements of the array, as they do not exist.
I have code, where I "think" I have now implemented this usage. I'll need to check.

John
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Wed Aug 25, 2010 8:32 am    Post subject: Reply with quote

Thanks John. This being the case, FTN95 is correct in this respect.
Back to top
View user's profile Send private message AIM Address
simon



Joined: 05 Jul 2006
Posts: 299

PostPosted: Wed Aug 25, 2010 2:17 pm    Post subject: Reply with quote

Thanks to all. I am wondering whether the line
Code:
a(:,i) = i

could potentially overwrite some other memory. I guess this may be compiler specific, but does anything actually happen in this do loop?
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Wed Aug 25, 2010 7:38 pm    Post subject: Reply with quote

Nothing happens because the do loop is not entered.
The test comes first and is false.
There are no items in the array to assign a value to.
Back to top
View user's profile Send private message AIM Address
Sebastian



Joined: 20 Feb 2008
Posts: 177

PostPosted: Thu Aug 26, 2010 10:11 am    Post subject: Reply with quote

Quote:
Nothing happens because the do loop is not entered.

What do-loop is not entered? The one in the posted code snippet surely is (upper loop boundary is 5).



Quote:
Thanks to all. I am wondering whether the line
a(:,i) = i
could potentially overwrite some other memory.

I don't remember if
Code:
a(:)
is affected as well or if twodimensional arrays are special with regard to the check issue, but
Code:
a=0
(used by times in initialization routines) DOES corrupt memory for a zero-allocated array, see the linked posting.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Thu Aug 26, 2010 2:07 pm    Post subject: Reply with quote

As I have already indicated in this thread the do loop is not the explicit do loop in the code but the one supplied by the compiler in order to expand
a(:,i) = i.

Quote:
a(:,i) = i is expanded as a do loop that is not entered
Back to top
View user's profile Send private message AIM Address
Sebastian



Joined: 20 Feb 2008
Posts: 177

PostPosted: Thu Aug 26, 2010 3:17 pm    Post subject: Reply with quote

Quote:
As I have already indicated in this thread the do loop is not the explicit do loop in the code

This was not apparent to me from the context, thanks for clarification.


Quote:
Nothing happens because the do loop is not entered.
The test comes first and is false.
There are no items in the array to assign a value to.

So the : qualified argument has no problem, but the unqualified (a=0). The latter occurring a lot in legacy code here, hard to track.
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Fri Aug 27, 2010 1:53 am    Post subject: Reply with quote

Sebastian,

Where does "a = 0" fail?
Your link had the problem of addressing an array which was not allocated, not an allocated array of size zero.
I'd be interested to find out if a zero size allocated array produced the problem, as I have coded this and not found the problem, as yet.
Your code below, as modified does not show any apparent problems for me.

John
Code:
program alloc
    integer, allocatable, dimension(:) :: array
!
    allocate (array(0))
    array = 0
    write (*,*) size(array)
    write (*,*) 'Array :',array
    print*, "Should not reach this."
end program alloc
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Sun Aug 29, 2010 7:38 am    Post subject: Reply with quote

I don't think there is any problem. Zero size arrays have been added as a new feature since Fortran 90. The ALLOCATE statement produces an array of zero size.

The loop

Code:

DO i=1,m
   a(:,i) = i
END DO


may be compiled as

Code:

i = m+1


The loop
Code:


DO k=1,n
   DO i=1,m
      a(k,i) = i
   END DO
END DO


may be compiled as

Code:

k = 1


The following statements do not generate any code.

Code:

a = 0
a(:,1:m) = 0


I think the FTN95 compiler does all these things correctly.
Back to top
View user's profile Send private message
Sebastian



Joined: 20 Feb 2008
Posts: 177

PostPosted: Sun Aug 29, 2010 2:41 pm    Post subject: Reply with quote

Quote:
Where does "a = 0" fail?
Your link had the problem of addressing an array which was not allocated, not an allocated array of size zero.

You are right, the crash only happens if the array is unallocated and the file is compiled using non-check/non-checkmate parameters. The bug is that check/checkmate does not trigger an error.
But it's unrelated to the zero-sized arrays which is the topic of the thread.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group