Use the operator fixed
:
fixed (double * aaPtr = aa) {
// You can use the pointer in here.
}
fixed
, .
:
public class EigenSolver
{
public double[] _aa;
/*
There really is no reason to allow callers to pass a pointer here,
just make them pass the array.
public EigenSolver(double* ap)
{
aPtr = ap;
}
*/
public EigenSolver(double[] aa)
{
_aa = aa;
}
public void Solve()
{
unsafe {
fixed (double* ptr = _aa) {
Interop.CallFortranCode(ptr);
}
}
}
}
, , , CallFortranCode . , ...
UPDATE:
double[] aa
. , GC , .
, : Marshal.AllocHGlobal (aa.Length * sizeof(double))
). Marshal.Copy :
bool _ownsPointer;
public EigenSolver(double[] aa) {
IntPtr arrayStore = (double*)Marshal.AllocHGlobal(aa.Length * sizeof(double));
Marshal.Copy(aa, 0, arrayStore, aa.Length);
this.aPtr = (double*)arrayStore.ToPointer();
_ownsPointer = true;
}
~EigenSolver {
if (_ownsPointer) {
Marshal.FreeHGlobal(new IntPtr(this.aPtr));
}
}
, ...