C++访问违规行为到Fortran阵列

发布于 2025-02-11 17:26:27 字数 1286 浏览 0 评论 0原文

我正在从事C ++ - Fortran Mix编译项目。在Fortran方面,我编写了一个接口,将C ++参考/指针分离为Fortran变量。在Fortran子例程完成他们的工作之后,接口将对C ++数组一一分配值。当问题回到C ++方面时,就会发生问题。我可以在C ++调试器中看到每个数组元素的值,但是当我使用std :: cout<< arr [0]<<<<< en endl时,它给了我访问错误:

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

我写了一个简化的测试代码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!
}

相同的例外报告,即使我打印出与Fort_Interface()没有业务的“检查”。同时,调试器中的“检查”值为12321。

我写了另一个玩具Fortran-C ++代码,以测试Fortran如何使用指针/参考,一切顺利。价值可以打印,驱动器也可以很好地工作。

抱歉,我无法在此处上传fortran-c ++界面,因为它包含100个以上的参数(这就是为什么我编写此界面以使Fortran原始代码完好无损地使用外部参数。)我只想知道为什么我可以在调试中看到该值但是它无法访问吗?感谢您的帮助!


更新:问题解决了!我将指向指向Fortran子例程的指针进行了指示,该指针导致了段错误。 Fortran-C Mix编译设置没有错。仅仅因为段错误。再次感谢大家!

I am working on a C++-fortran mix compiling project. On the fortran side, I wrote an interface to segregate the C++ reference/pointers to fortran variables. After the fortran subroutine did their work, the interface will assign the value one-by-one to the C++ array. The problem occurred when it went back to C++ side. 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:

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(). At the same time, the value of "check" was 12321 in the debugger.

I wrote another toy fortran-C++ code to test how fortran play with pointers/references, everything goes well. 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? 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. Nothing wrong about fortran-C mix compile setting. Just because of the segment error. Thank you all again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2025-02-18 17:26:27

由于我看不到您的Fortran-C ++接口,因此我会猜测这可能会有所帮助。

当您在Fortran中处理数组时,它们将作为列保存在您的内存中,而C/C ++将其保存为行。这使这两种语言之间的接口很棘手。

例如,如果您具有ARR [5] [5],并且在C中调用元素ARR [1] [0],则编译器将获取第1行并将其保存在您的缓存中,而Fortran编译器将fetch列0和将其保存在您的缓存中。

我希望这会有所帮助

Since I can't see your Fortran-C++ interface, I will make a guess that might help.

When you process arrays in fortran, they are saved as columns in your memory, while C/C++ saves them as rows. 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.

I hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文