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

C++ and FORTRAN strings on x64

chris_biddlecombe
619 Views

I have a problem which occurs on x64 but not win32. I have a c++ routine which declares a character string

char string[16]

and passes it to fortran which gives string a value

string = 'abc'

This results in an error: "A buffer overrun has occurred in post.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program."

If I change the code to

string(1:len(string)) = 'abc'

the error changes to "Variable STRING has substring ending point 16 which is greater than the variable length of 16".

I cannt reproduce either error in 10 lines of code, so it's difficult for support to help me. Any ideas or work-arounds would be most welcome.

0 Kudos
5 Replies
TimP
Honored Contributor III
619 Views

If your C++ code expects a null terminated string, you should initialize it accordingly. Aside from special facilities of ifort, you might use 'abc'//achar(0)

You might have been lucky in the 32-bit version to have an acceptable terminating character appear in the buffer.

I'm guessing, as that's about all that can be done with the information you presented.

0 Kudos
jimdempseyatthecove
Honored Contributor III
619 Views

How is string declared on the Fortran side?

If it is (0:15) as opposed to (1:16) then you may see this problem.

The IFORT documentation is not clear on this issue. I suggest placing a break point on the assignment causing the problem. Then examin STRING in the debugger to find the array indicies (see if it is 0:15 or 1:16, or something else).

Jim

0 Kudos
chris_biddlecombe
619 Views
stringis declared as character*(*) in the fortran,the real length being passed as a hidden argument. The LEN function returns the correct value (16). The debugger knows that the argument to the routine is CHARACTER(16). I cannot see any way of looking at the string indices in the debugger. However, if I try to set characters 1 to 15 (which should be safe, it tells me that "substring ending point 15 is greater than 16"!
0 Kudos
chris_biddlecombe
619 Views
Fortran should not need a null character to tell it how long string is. The debugger knows it is CHARACTER(16) and the LEN function gets that right too. But how can 16 be greater than 16? If I try setting characters 1 to 15, it tells me that 15 is greater than 16!
0 Kudos
Jugoslav_Dujic
Valued Contributor II
619 Views
Chris.Biddlecombe@vectorfields.co.uk:
The debugger knows it is CHARACTER(16) and the LEN function gets that right too. But how can 16 be greater than 16? If I try setting characters 1 to 15, it tells me that 15 is greater than 16!


Try 17, then Smiley with tongue out [:-P]

But seriously, this now sounds like an issue for Premier support; either you discovered a compiler bug, or you made a fandango on core elsewhere.
0 Kudos
Reply