Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28868 Discussions

Access Violation Passing A Function As Argument

Petri__Fabrizio
799 Views

Hi everybody. I found an access violation using a function, passed as argument inside another function.

The code is this:

      function NRF_Hn(x, F_, G_, h)  result(Hn)
      
         implicit none
         double precision                        , pointer :: Hn(:,:)
         double precision,           intent(in) :: x(:)
         double precision, optional, intent(in) :: h(:)
         procedure(INRFunctionDouble), optional :: F_
         procedure(INRGradientDouble), optional :: G_
         
         double precision, allocatable :: xplush(:)
         double precision, allocatable :: xminush(:)
         double precision, pointer :: gMinus(:)
         double precision, pointer :: gPlus(:)
         integer  n , i
         
         n = size(x)
         allocate(Hn(n,n))
         Hn = 0.0d0
         
         !Creates x+h and x-h as Vectors
         allocate(xPlusH(n))
         allocate(xMinusH(n))

         
         !Copy the x in Vectors
         xPlusH  = x
         xMinusH = x
         
                 
          do i = 1 , n
            
            xPlusH(i)   =  xPlush(i)  + H(i)
            xMinusH(i)  =  xMinush(i) - H(i)
                        
            gMinus =>  G_(xMinusH, F_,h)  !<------ Access Violation Here !!!!
            gPlus  =>  G_( xPlusH, F_,h)
                       

                        
            Hn(:,i) = (gPlus - gMinus)/(2.0d0*h(i))     
            
            xPlusH(i)   =  x(i) 
            xMinusH(i)  =  x(i) 
            
          end do   
          

         
         deallocate(xPlusH)
         deallocate(xMinusH)
                  
      end function

This function is inside a class module.

 I attached the minimum project that reproduce the error, can you help in understanding what's going wrong?

0 Kudos
1 Solution
Petri__Fabrizio
799 Views

Hi Markus, thank you for your remarks. 

You are right. I stumbled a compiler bug. I escalated it to Intel compiler developers. It will be fixed in Intel Compiler XE version 19.1

Regards

Fabrizio

View solution in original post

0 Kudos
3 Replies
Petri__Fabrizio
799 Views

Note that if change      

function H_(This, x)
        implicit none
        class(NewtonRaphsonFunctionClass)                 :: This
        double precision,pointer                          :: H_(:,:)
        double precision,   intent(in)  :: x(:)
         
        
        
         
        !H_  => This.m_H_(x, This.m_F_, This.m_G_, This.m_eps)
        H_  => This.m_H_(x, This.m_F_, NRF_GN, This.m_eps)     !<-- If I pass directly the function rather than the pointer to the function  This.m_G_  it Works!!
        
      end function  

 

It seams that for some reason the pointer This.m_G_ is corrupted. But if I  call This.m_G_ outside the function H_ it works. I can't explain this.

0 Kudos
Arjen_Markus
Honored Contributor I
799 Views

Just a few remarks:

  • You are using a dot instead of a percent sign to separate components - it is an extension to the compiler.
  • The & in defining the parameter confused me - you are defining the .f90 source file as fixed form, so that works, but it is confusing.
  • You define functions F_ and G_ but you do not use them. This is also a bit confusing.

I built the program using gfortran, but the resulting program works fine. You may have stumbled on a compiler bug.

0 Kudos
Petri__Fabrizio
800 Views

Hi Markus, thank you for your remarks. 

You are right. I stumbled a compiler bug. I escalated it to Intel compiler developers. It will be fixed in Intel Compiler XE version 19.1

Regards

Fabrizio

0 Kudos
Reply