[Bug fortran/47844] New: Pointer-valued function: Provide wrong result when dereferenced automatically after list-write

2011-02-21 Thread Kdx1999 at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47844

   Summary: Pointer-valued function: Provide wrong result when
dereferenced automatically after list-write
   Product: gcc
   Version: 4.6.0
Status: UNCONFIRMED
  Severity: minor
  Priority: P3
 Component: fortran
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: kdx1...@gmail.com


Hello,I'm trying to compile Example 15.20 in Stephen J.Chapman's book Fortran
95/2003 for Scientists & Engineers. The purpose of the function is simple:
"Return a pointer to every fifth element in a input rank 1 array".
--
--
Code:

PROGRAM test_pointer_value
  !
  ! Purpose:
  ! Test pointer valued function
  !
  ! Record of revisions:
  ! Date  Programmer  Description of change
  ! 02/22/2011KePuOriginal code
  !
  IMPLICIT NONE

  ! Data dictionary
  INTEGER,DIMENSION(10),TARGET::array=[1,3,5,7,9,11,13,15,17,19]! Array to be
test
  INTEGER,dimension(2)::arrar_fifth
  INTEGER,POINTER,DIMENSION(:)::ptr_array=>NULL()! Pointer to array
  INTEGER,POINTER,DIMENSION(:)::ptr_array_fifth=>NULL()  ! Pointer return every
fifth element of array

  ptr_array=>array  ! Initialization

  ptr_array_fifth=>every_fifth(ptr_array)
  WRITE(*,*)ptr_array_fifth
  WRITE(*,*)every_fifth(ptr_array)
CONTAINS
  FUNCTION every_fifth(ptr_array) RESULT(ptr_fifth)
!
! Purpose:
! To produce a pointer ot every fifth element in an
! input rand 1 array.
!
! Record of revisions:
! Date  Programmer  Description of change
! 02/22/2011KePuOriginal code
!
IMPLICIT NONE

INTEGER,POINTER,DIMENSION(:)::ptr_fifth
INTEGER,POINTER,DIMENSION(:),INTENT(in)::ptr_array
INTEGER::low
INTEGER::high

low=LBOUND(ptr_array,1)
high=UBOUND(ptr_array,1)
ptr_fifth=>ptr_array(low:high:5) 
  END FUNCTION every_fifth
END PROGRAM test_pointer_value 
--
--

The book says "The function can also be used in a location where an integer
array is expected. Inthat case, the pointer returned by the function will
automatically be dereferenced,and will print out the value by the pointer
returned from the function". But after ran the program, two result prompt on
the screen are different(The first line is right answer):
1   11
13

I'm not sure if it's a bug. Any help to my problem will be appreciated. Thank
you.


[Bug fortran/47845] New: Polymorphic deferred function: Not matched class

2011-02-21 Thread Kdx1999 at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47845

   Summary: Polymorphic deferred function: Not matched class
   Product: gcc
   Version: 4.6.0
Status: UNCONFIRMED
  Severity: major
  Priority: P3
 Component: fortran
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: kdx1...@gmail.com


I'm trying to work out an Exercise in Stephen Chapman's book Fortran 95/2003
for Scientists & Engineers. Create a abstract class vec and subclass vec2d and
vec3d, override some deferred functions, then test the classes.



Here is my construction of vec:
MODULE class_vec
  !
  ! Brief Description:
  ! 1. Superclass of vec2d and vec3d 
  ! 2. Perform vector addition and subtraction
  ! 3. Perform vector dot product
  !
  ! 4. Common fields:
  !  a.x
  !  b.y
  !  
  ! Record of revisions:
  ! Date  Programmer  Description of change
  ! 02/21/2011KePuOriginal code
  !
  IMPLICIT NONE

  TYPE,ABSTRACT::vec
 ! Common fields
 REAL::x
 REAL::y

 ! Declare methods
   CONTAINS
 GENERIC::OPERATOR(+)=>add
 GENERIC::OPERATOR(-)=>subtract
 GENERIC::OPERATOR(*)=>dot
 PROCEDURE,PASS::set_vec=>set_vec_sub
 PROCEDURE(addx),PASS,DEFERRED::add
 PROCEDURE(subtractx),PASS,DEFERRED::subtract
 PROCEDURE(dotx),PASS,DEFERRED::dot

  END TYPE vec

!!
!!

  ! Interfaces to deferred procedures
  ABSTRACT INTERFACE

 FUNCTION  addx(this,other) RESULT(add_vec)
   !
   ! Purpose:
   ! Add two vector
   !
   ! Record of revisions:
   ! Date  Programmer  Description of change
   ! 02/21/2011KePuOriginal code
   !
   IMPORT vec 
   IMPLICIT NONE
   CLASS(vec),INTENT(in)::this! This object
   CLASS(vec),INTENT(in)::other   ! The other object
   CLASS(vec),POINTER::add_vec! Return value

 END FUNCTION addx

!!

 FUNCTION subtractx(this,other) RESULT(subtract_vec)
   !
   ! Purpose:
   ! Subtract two vector
   !
   ! Reord of revisions:
   ! Date  Programmer  Description of change
   ! 02/21/2011KePuOriginal code
   !
   IMPORT vec
   IMPLICIT NONE
   CLASS(vec),INTENT(in)::this! This object
   CLASS(vec),INTENT(in)::other   ! The other object
   CLASS(vec),Pointer::subtract_vec   ! Return value
 END FUNCTION subtractx

!!

 FUNCTION dotx(this,other)
   !
   ! Purpose:
   ! Dot product of two vectors
   !
   ! Record of revisions:
   ! Date  Programmer  Description of change
   ! 02/21/2011KePuOriginal code
   !
   IMPORT vec
   IMPLICIT NONE
   CLASS(vec),INTENT(in)::this! This object
   CLASS(vec),INTENT(in)::other   ! The other object
   REAL::dotx ! Return value
 END FUNCTION dotx
  END INTERFACE

!!
!!

  ! Define methods
CONTAINS

  SUBROUTINE set_vec_sub(this,x,y)
!
! Purpose:
! Set coordinate of vector
!
! Record of revisions:
! Date  Programmer  Description of change
! 02/21/2011KePuOriginal code
!
IMPLICIT NONE

! Data dictionary:
CLASS(vec),INTENT(inout)::this ! Input object
REAL,INTENT(in)::x,y   ! Coordinate 

this%x=x
this%y=y 

  END SUBROUTINE set_vec_sub
END MODULE class_vec

--
--

Subclass vec3d will override all the functions and subroutines defined above

MODULE class_vec3d
  !
  ! Brief description:
  ! 1. Subclass of vec
  ! 2. Fields
  !  a. Inherited: real::x real::y
  !  b. Extends: real::z
  ! 3. Method
  !  a. set_vec
  !  b. Addition
  !  c. Subtraction
  !  d. Dot product
  !
  ! Record of revisions:
  ! Date  Programmer  Description of change
  ! 02/22/2011KePuOriginal code
  !
  USE class_vec ! Use parent class

  IMPLICIT NONE

  ! Type definition
  TYPE,EXTENDS(vec),PUBLIC::vec3d
 ! Fields
 REAL::z

 ! Declare methods
   CONTAINS
 PROCEDURE,PUBLIC,PASS::set_vec=>set_vec_3d
 PROCEDURE,PUBLIC,PASS::add=>add_fn
 PROCED

[Bug fortran/47845] [OOP] Polymorphic deferred function: Not matched class

2011-02-22 Thread Kdx1999 at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47845

--- Comment #2 from KePu  2011-02-22 12:54:10 UTC ---
Thank you for respond this issue, I have modified the definition of vec:). But
it seems that only the first argument can be set to subclass of vec, the other
must be strictly set to vec, so the additional coordinate z can't be used in
newly overriding procedures.