gpt4 book ai didi

c# - 数组大小超出寻址限制(C# 与 C++)

转载 作者:行者123 更新时间:2023-11-30 05:36:21 26 4
gpt4 key购买 nike

我有一个 C# 程序来管理复杂的巨大数组(大于 2GB)。该程序在 x64 中编译,在 App.config 中使用 gcAllowVeryLargeObjects=true 子句:

<configuration>
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
</runtime>
</configuration>

它需要使用第三方 dll,但是当将这些巨大的数组传递给这个外部函数时,编码似乎失败了。

这是异常“Array size exceeds addressing limitation

这似乎是一个 VS CLR 问题,因为我做了这个例子来演示它。

class Program
{
static void Main(string[] args)
{
MyWrapper.MyTest();
}
}

[SuppressUnmanagedCodeSecurity]
public class MyWrapper
{


private MyWrapper()
{
}

internal static int CS_foo(
[In] Complex[,] A // text to prepend to converted integer
)
{
Complex B_complex= new Complex(0, 0);
for (int i = 0; i< 10; i++)
{

B_complex = A[0, i];
Console.WriteLine(string.Format(" from C# A({0})\t{1}\ti{2}", i, B_complex.Real, B_complex.Imaginary));
}
return 0;
}

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[DllImport("DLL1.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
internal static extern int C_foo(
[In] Complex[,] A// text to prepend to converted integer

);


public static void MyTest()
{
Complex[,]A = new Complex[12000, 12000];

A[0, 0] = new Complex(0.1, -0.1);
A[0, 1] = new Complex(11.0, 0.1);
A[0, 5] = new Complex (55, -0.5);
A[0, 9] = new Complex (99.0, 0.9);

MyWrapper.CS_foo(A);
MyWrapper.C_foo(A); //Here Fails!!
}
}

C++ DLL 代码:

#include "stdafx.h"
#include "stdio.h"


#ifndef Complex16
typedef
struct Complex16 {
double real;
double imag;
} Complex16;
#endif

extern "C" int __declspec(dllexport) C_foo(
Complex16* A
)

{
Complex16 B;
for (int i = 0; i < 10; i++)
{
B.real = A[i].real;
B.imag = A[i].imag;
printf(" hello from C A(%d)=( %f %f ) \n ", i, B.real, B.imag);
}
return 0;
}

如果您更改数组大小(例如 1200x1200),则该示例有效!知道如何解决这个问题吗?

他京

最佳答案

可以在这里找到解决方案:https://social.msdn.microsoft.com/Forums/vstudio/en-US/b02c61ab-8be1-4ed1-9752-9fa24211d78a/c-vs-c-array-size-exceeds-adressing-limitation?forum=csharpgeneral

简而言之:

默认编码不喜欢这么大的数组。固定数组并将指向其第一个元素的指针传递给 native 函数:

using System.Numerics;
using System.Runtime.InteropServices;

public unsafe class MyWrapper
{
static void Main(string[] args)
{
Complex[,] A = new Complex[12000, 12000];

A[0, 0] = new Complex(0.1, -0.1);
A[0, 1] = new Complex(11.0, 0.1);
A[0, 5] = new Complex(55, -0.5);
A[0, 9] = new Complex(99.0, 0.9);

fixed (Complex* p = &A[0, 0])
C_foo(p);
}

[DllImport("DLL1.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
internal static extern int C_foo(Complex* A);

关于c# - 数组大小超出寻址限制(C# 与 C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33607447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com