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

Access violation writing while copying variable to variable

daninraleigh
Beginner
1,237 Views
I'm getting an access violation while copying value from one variable to another variable. Here is the error message I receive:

Unhandled exception at 0x00494a5a in TEST.exe: 0xC0000005: Access violation writing location 0x004fa7d4.

And here is the code. The error appears while running the following line of code:
[bash]STR(I:I) = ACHAR(IACHAR(STR(I:I)) - DEL)[/bash]

Full subroutine below:

[bash]      SUBROUTINE UPPERCASE(STR)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN OUT) :: STR
      INTEGER :: I, DEL


      DEL = IACHAR('a') - IACHAR('A')

      DO I = 1, LEN_TRIM(STR)
         IF (LGE(STR(I:I),'a') .AND. LLE(STR(I:I),'z')) THEN
            STR(I:I) = ACHAR(IACHAR(STR(I:I)) - DEL)
         END IF
      END DO

      RETURN

      END SUBROUTINE UPPERCASE
[/bash]

Any ideas? Maybe I have a setting incorrect or something else?
0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,237 Views
Please supply a main program which calls this routine and gets the error.

View solution in original post

0 Kudos
5 Replies
rasa
Beginner
1,237 Views
When it comes to string conversion there are a ton of code avaialbe in public domain. Just google it. Maybe the following will help you to debug (from a c.l.f. discussion).

[bash]MODULE STRINGS

IMPLICIT NONE
!======================================================================
FUNCTION TOUPPER(sString)

CHARACTER(*),INTENT(IN)::     sString
CHARACTER(LEN=LEN(sString)):: TOUPPER

INTEGER::                     i, iAsc

TOUPPER=sString
DO i=1,LEN(sString)
      iAsc=ICHAR(sString(i:i))
      IF (iAsc.GE.97 .AND. iAsc.LE.122) TOUPPER(i:i)=CHAR(iAsc-32)
END DO

END FUNCTION TOUPPER
!======================================================================
FUNCTION TOLOWER(sString)

CHARACTER(*),INTENT(IN)::     sString
CHARACTER(LEN=LEN(sString)):: TOLOWER

INTEGER::                     i, iAsc

TOLOWER=sString
DO i=1,LEN(sString)
      iAsc=ICHAR(sString(i:i))
      IF (iAsc.GE.65 .AND. iAsc.LE.90) TOLOWER(i:i)=CHAR(iAsc+32)
END DO

END FUNCTION TOLOWER
!======================================================================
END MODULE STRINGS [/bash]

People also use the INDEX function for case conversion. This is independent of the character representation (ASCII, DISPLA. EBCDIC, etc.). It works as:

[bash]elemental function str_toupper ( inpstring ) result( outstring )
implicit none

character(len = 26), parameter:: LOWER_CASE = 'abcdefghijklmnopqrstuvwxyz'
character(len = 26), parameter:: UPPER_CASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 

! -- Argument and result
character(len = *), intent (in) :: inpstring
character(len = LEN(inpstring)) :: outstring

! -- Local variables
integer :: ii, n

! -- Copy input string
outstring = inpstring

! -- Loop over string elements
do ii = 1, LEN(outstring)
  ! -- Find location of letter in lower case constant string
  n = INDEX( LOWER_CASE, outstring( ii:ii ) )

  ! -- If current substring is a lower case letter, make it upper case
  if ( n /= 0 ) outstring( ii:ii ) = UPPER_CASE( n:n )
end do

end function str_toupper[/bash]

Take a look at http://flibs.sourceforge.net/.

HTH.
0 Kudos
Steven_L_Intel1
Employee
1,238 Views
Please supply a main program which calls this routine and gets the error.
0 Kudos
Lorri_M_Intel
Employee
1,237 Views
I seeyou are converting in place; are you passing a variable?

Or was it a string-constantsuch as:

CALL UPPERCASE ("Hello!")

By default, string-constants are put into read-onlymemory.

--- Lorri
0 Kudos
daninraleigh
Beginner
1,237 Views
Steve,
Yeah, good suggestion. I took the EXE in question and ripped everything out. Left all the settings the same, but ripped everything out to call just that subroutine and it works. So, I need to find out what is offending it. Thanks.

Lorri,
Just an FYI - I'm passing a variable and it works in the stripped down version that way. Thanks for the advice.


--Dan
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,237 Views
"Access violation writing location" means you wereattempting to write to anaddress that is eitherwrite protected or not mapped to your virtual address (and is not automatically mapped asin first write circumstance). Your code contains a read of the string location STR(I:I)prior to write and youdid not receive "Accessviolation reading location". Therefore it appears that STR(I:) was readable but not writeable. Depending onthe version of Windowsyou canhave virtual memory pages that are marked Execute Only (implies Read Only) as well as Read Only. Pages that you cannot write to might, for example, contain string literals. On your program that exhibits this error, add a WRITE(*,*) STR before your DO loop. See what comes out.

Jim Dempsey
0 Kudos
Reply