2 Replies Latest reply: Aug 1, 2014 4:17 AM by 2722955 RSS

    Allocatable Arrays created with incorrect rank in Sun Studio 12.1

    2722955

      Show below is a simple Fortran subroutine, which when called, allocates a small amount of space to a character array.....

       

            SUBROUTINE allocate_test

       

            CHARACTER*21 data_mask

            CHARACTER,DIMENSION(:), ALLOCATABLE::trimmed_mask

            INTEGER howBig

            data_mask = '/data/products/tests/'

       

            allocate(trimmed_mask(21))

            howBig = size(trimmed_mask,1)

            trimmed_mask = data_mask

            deallocate(trimmed_mask)

            RETURN

            END

       

      When this is run the 'trimmed_mask' array appears to be an array of rank 2 not 1 as expected. This is shown in the image below ...

       

       

      error.gif

       

       

      ... notice how trimmed_mask is defined as being 'trimmed_mask(1:1,1:21). In addition the assignment of a value to the allocated array forces the first character of the assigned array in to each element of the allocated array. Anyone have an idea as why this is happening? The rank of the allocated array appears to be correctly defined?

       

      Thanks

        • 1. Re: Allocatable Arrays created with incorrect rank in Sun Studio 12.1
          user12616298

          This is a disconnect between the debug information the fortran compiler produces and dbx.

          Internally, the fortran compiler treats an array of character(len) as a 2D array (1:len(var), 1:size(var))

           

          The interaction between fortran and dbx has been corrected in solarisstudio12.3.  There dbx correctly identifies trimmed_mask as an allocatable array of character.

          (dbx) whatis trimmed_mask

          character*1, allocatable::trimmed_mask(1:21)

           

          Regarding the behavior you see on the assignment, the behavior of the fortran compiler is correct.  You have not declared a length 21 character string, you've declared an array of 21 length 1 character strings.  When performing an array assignment: array = scalar, each element of the array is assigned the scalar value.  Fortran semantics for character assignment is to truncate the right hand side to fit the left hand side, so each element of the array is assigned '/'.

           

          What you probably wanted to use was a deferred length character string.  Unfortunately solaris studio won't support deferred length characters until the next release.

           

            CHARACTER*21 data_mask
            CHARACTER(:), ALLOCATABLE::trimmed_mask
            INTEGER howBig
            data_mask = '/data/products/tests/'
            allocate(character(21):: trimmed_mask)
            howBig = len(trimmed_mask)
            trimmed_mask = data_mask
            deallocate(trimmed_mask)
            END

           

           

          Another possibility is to use a scalar allocatable.  Scalar allocatables are supported in solarisstudio12.2, although the interaction with dbx seems not to have been fixed til solarisstudio12.3

           

            CHARACTER*21 data_mask
            CHARACTER(21), ALLOCATABLE::trimmed_mask
            INTEGER howBig
            data_mask = '/data/products/tests/'
            allocate(trimmed_mask)
            howBig = len(trimmed_mask)
            trimmed_mask = data_mask
            deallocate(trimmed_mask)
            END
          • 2. Re: Allocatable Arrays created with incorrect rank in Sun Studio 12.1
            2722955

            Thanks for this - very helpful