开发者

Passing c arrays into fortran as a variable sized matrix

开发者 https://www.devze.com 2023-04-03 09:53 出处:网络
So, i\'ve been commissioned to translate some fortran subroutines into C.These subroutines are being called as part of the control flow of a large porgram based primarily in C.

So, i've been commissioned to translate some fortran subroutines into C. These subroutines are being called as part of the control flow of a large porgram based primarily in C.

I am translating the functions one at a time, starting with the functions that are found at the top of call stacks.

The problem I am facing is the hand-off of array data from C to fortran.

Suppose we have declared an array in c as

int* someCArray = (int*)malloc( 50 * 4 * sizeof(int) );

Now, this array needs to be passed down into a fortran subroutine to be filled with data

someFortranFunc( some开发者_如何学PythonCArray, someOtherParams );

when the array arrives in fortran land, it is declared as a variable sized matrix as such:

subroutine somefortranfunc(somecarray,someotherparams)
integer somefarray(50,*)

The problem is that fortran doesn't seem to size the array correctly, becuase the program seg-faults. When I debug the program, I find that indexing to

somefarray(1,2)

reports that this is an invalid index. Any references to any items in the first column work fine, but there is only one available column in the array when it arrives in fortran.

I can't really change the fact that this is a variable sized array in fortran. Can anyone explain what is happening here, and is there a way that I can mitigate the problem from the C side of things?

[edit]

By the way, the fortran subroutine is being called from the replaced fortran code as

integer somedatastorage(plentybignumber)
integer someindex
...
call somefarray(somedatastorage(someindex))

where the data storage is a large 1d array. There isn't a problem with overrunning the size of the data storage. Somehow, though, the difference between passing the C array and the fortran (sub)array is causing a difference in the fortran subroutine.

Thanks!


Have you considered the Fortran ISO C Binding? I've had very good results with it to interface Fortran and C in both directions. My preference is to avoid rewriting existing, tested code. There are a few types that can't be transferred with the current version of the ISO C Binding, so a translation might be necessary.


What it shouldn't be that others suggested: 1. Size of int vs. size of Integer. Since the first column has the right values. 2. Row vs. column ordering. Would just get values in wrong order not segmentation faulted. 3. Reference vs value passing. Since the first column has the right values. Unless the compiler is doing something evil behind your back.

Are you sure you don't do this in some secret way?:

   someCArray++

print out the value of the someCArray pointer right after you make it and right before you pass it. You also should print it out using the debugger in the fortran code just to verify that the compiler is not generating some temporary copies to help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消