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

BIND(C) and a function taking and returning a C_PTR

IanH
Honored Contributor III
525 Views
The following Fortran source has a function that takes a C_PTR (by value) and returns a C_PTR. It has a companion C main program that can be used to call the function.

bind-c-change-fortran.f90
bind-c-change-c.c

When compiled with 11.1.067, linked with the C main program and run, I get both a warm glow of satisfaction and three lines on the screen with the same hexadecimal representation of the "address pointed to".

[plain]>ifort /Od /debug:full /warn:all /check:all /MDd /c bind-c-change-fortran.f90
Intel Visual Fortran Compiler Professional for applications running on IA-32,
 Version 11.1    Build 20100806 Package ID: w_cprof_p_11.1.067
Copyright (C) 1985-2010 Intel Corporation.  All rights reserved.

>cl /Od /Zi /W4 /RTC1 /MDd /TC /c bind-c-change-c.c
Microsoft  32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86

Copyright (C) Microsoft Corporation.  All rights reserved.

bind-c-change-c.c

>cl bind-c-change-c.obj bind-c-change-fortran.obj /Febind-c-change.exe
Microsoft  32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86

Copyright (C) Microsoft Corporation.  All rights reserved.

Microsoft  Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:bind-c-change.exe
bind-c-change-c.obj
bind-c-change-fortran.obj

>bind-c-change.exe
ptr in main: 0012FF60
arg in fortran_proc: 0012FF60
rc in main: 0012FF60[/plain]


When compiled/linked/run with 12.0.1.127 in a similar manner, the last two address representations are different from the first representation, the program crashes on exit (with appropriate compile/link/run-under-debugger the runtime will throw a tantrum about stack pointer corruption) and I start feeling like I really need a good lie-down.

[plain]>ifort /Od /debug:full /warn:all /check:all /MDd /c bind-c-change-fortran.f90
Intel Visual Fortran Compiler XE for applications running on IA-32, Version 1
2.0.1.127 Build 20101116
Copyright (C) 1985-2010 Intel Corporation.  All rights reserved.

>cl /Od /Zi /W4 /RTC1 /MDd /TC /c bind-c-change-c.c
Microsoft  32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86

Copyright (C) Microsoft Corporation.  All rights reserved.

bind-c-change-c.c

>cl bind-c-change-c.obj bind-c-change-fortran.obj /Febind-c-change.exe
...

>bind-c-change.exe
ptr in main: 0012FF60
arg in fortran_proc: 01CB9B48
rc in main: 01CB9B48

[/plain]
Should I go and have a nap in the corner?
0 Kudos
3 Replies
Steven_L_Intel1
Employee
525 Views
I can reproduce this and will investigate further.
0 Kudos
Steven_L_Intel1
Employee
525 Views
I can see at least two things wrong here. The first is that the 12.0 Fortran code is picking up the argument from the wrong location, as if it were a second argument. The second is that it is corrupting the stack on exit, using a "RET 4", as if it were STDCALL, instead of "RET".

I have escalated this as issue DPD200164328 and will let you know of the progress.
0 Kudos
Steven_L_Intel1
Employee
525 Views
I expect the fix for this to appear in Update 3.
0 Kudos
Reply