简体   繁体   English

从 C++ 到 fortran 数组的访问冲突

[英]access violation from C++ to fortran array

I am working on a C++-fortran mix compiling project.我正在开发一个 C++-fortran 混合编译项目。 On the fortran side, I wrote an interface to segregate the C++ reference/pointers to fortran variables.在 fortran 方面,我编写了一个接口来隔离 C++ 引用/指向 fortran 变量的指针。 After the fortran subroutine did their work, the interface will assign the value one-by-one to the C++ array.在 fortran 子例程完成它们的工作后,接口会将值一一分配给 C++ 数组。 The problem occurred when it went back to C++ side.回到 C++ 端时出现问题。 I can see the value of each array elements in the C++ debugger, but when I use std::cout<<arr[0]<<std::endl, it gives me the access error:我可以在 C++ 调试器中看到每个数组元素的值,但是当我使用 std::cout<<arr[0]<<std::endl 时,它给了我访问错误:

Exception thrown at 0x79762B8E (msvcp140d.dll) in TEST_IO.exe: 0xC0000005: Access violation reading location 0x9C6D0014

I wrote a simplified test code as:我写了一个简化的测试代码:

#include <iostream>
extern "C" {void fort_interface(<typeName1> &par1,<typename2> *par2,....,<typeName> *output);}
int main()
{
..... //setup input parameters
float *arr = new float[N_arr]; //N_arr is big enough.

float check = 12321;
std::cout<<check<<std::endl;
/*the float variable check is independent from fort_interface, just to check the memory status*/

fort_interface(par1, par2,...,arr);

std::cout<<check<<std::endl;//exception occurs!
}

The same exception reported even I print out "check" that did no business with fort_interface().即使我打印出与 fort_interface() 无关的“检查”,也会报告相同的异常。 At the same time, the value of "check" was 12321 in the debugger.同时在调试器中“check”的值为12321。

I wrote another toy fortran-C++ code to test how fortran play with pointers/references, everything goes well.我写了另一个玩具 fortran-C++ 代码来测试 fortran 如何使用指针/引用,一切顺利。 Value can be printed, destructor works well too.可以打印值,析构函数也可以很好地工作。

Sorry that I cannot upload the fortran-C++ interface here because it contains 100+ parameters (that's the reason why I wrote this interface to keep fortran original code intact with outside argument.) I just want to know why I can see the value in debugger but it cannot be accessed?抱歉,我无法在此处上传 fortran-C++ 接口,因为它包含 100 多个参数(这就是我编写此接口以使用外部参数保持 fortran 原始代码完整的原因。)我只想知道为什么我可以在调试器中看到该值但它无法访问? Thank you for your help!谢谢您的帮助!


Update: The problem was solved!更新:问题解决了! I passed a pointer to pointer to the fortran subroutine, which leaded the segment error.我传递了一个指向 fortran 子例程的指针,这导致了段错误。 Nothing wrong about fortran-C mix compile setting. fortran-C 混合编译设置没有错。 Just because of the segment error.只是因为段错误。 Thank you all again!再次感谢大家!

Since I can't see your Fortran-C++ interface, I will make a guess that might help.由于我看不到您的 Fortran-C++ 接口,因此我猜测可能会有所帮助。

When you process arrays in fortran, they are saved as columns in your memory, while C/C++ saves them as rows.在 fortran 中处理数组时,它们在内存中保存为列,而 C/C++ 将它们保存为行。 which makes the interface between these two languages tricky.这使得这两种语言之间的接口变得棘手。

For example, if you have arr[5][5] and you call the element arr[1][0] in C, the compiler will fetch the row 1 and keep it in your cache, while fortran compiler will fetch column 0 and keep it in your cache.例如,如果您有 arr[5][5] 并在 C 中调用元素 arr[1][0],编译器将获取第 1 行并将其保存在缓存中,而 fortran 编译器将获取第 0 列并将其保存在您的缓存中。

I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM