replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Module protected pointer
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 

Module protected pointer

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



Joined: 18 May 2012
Posts: 813
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Sat May 17, 2025 11:07 am    Post subject: Module protected pointer Reply with quote

Often I have an array passed as a dummy argument to a procedure in a module. In order to make that array available to all other procedures in the module a local copy is made as a module variable. This doubles the memory allocation.

For large arrays associating a pointer to the input array would avoid this extra memory allocation. I have been experimenting with this alternative to try and understand the potential pitfalls. This has revealed an issue with module protected pointers.

In the following code since the shared_array has the protected attribute the assigment shared_array(5) = 100 in the main program should generate a compiler error.

Rather a "twisted" example, since only the subroutines load_array and use_array need to be public in my_mod, in which case FTN95 would correctly reject the code.

Code:
module my_mod
  implicit none
  real, pointer, protected :: shared_array(:)
!  real, pointer, private :: shared_array(:)  ! This would be better in this context.

contains

  subroutine load_array(arr)
    real, target, intent(in) :: arr(:)
    shared_array => arr    ! associate pointer to input array
    !shared_array(5) = 100  ! this assigment would correctly change arr despite its intent(in)
  end subroutine load_array

  subroutine use_array()
      print *, shared_array
  end subroutine use_array

end module my_mod

program p
use my_mod
implicit none
real :: a(5) = [1,2,3,4,5]

  call load_array(a)
 
  call use_array()       ! Prints 1,2,3,4,5 via pointer association
 
  shared_array(5) = 100  ! Since shared_array is protected, FTN95 should
                         ! not permit this assignment (since it's external
                         ! to MY_MOD).
  print*
  call use_array()       ! Prints 1,2,3,4,100 due to line above being permitted.
 
end program p
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


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

PostPosted: Sat May 17, 2025 11:21 am    Post subject: Reply with quote

Ken

Thank you for the bug report. I have logged this for investigation.
Back to top
View user's profile Send private message AIM Address
PaulLaidler
Site Admin


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

PostPosted: Sat May 17, 2025 12:34 pm    Post subject: Reply with quote

This failure has now been fixed for the next release of FTN95.
Back to top
View user's profile Send private message AIM Address
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