c++到Delphi的指针转换

C++ to Delphi pointer conversion

本文关键字:指针 转换 Delphi c++      更新时间:2023-10-16

我正在尝试将一个开源c++项目转换为Delphi(Berlin 10.1)。它使用了一些指针,我找不到一种方法将它们转换为Delphi指针。我怎么能翻译这块代码从c++到Delphi?下面是代码:

int SolveAll(int DragFunction, double DragCoefficient, double Vi, 
      double SightHeight, 
      double ShootingAngle, double ZAngle, double WindSpeed, double WindAngle, 
      double** Solution)
{    
    double* ptr;
    ptr = (double*)malloc(10*__BCOMP_MAXRANGE__*sizeof(double)+2048);
    double t=0;
    double dt=0.5/Vi;
    double v=0;
    double vx=0, vx1=0, vy=0, vy1=0;
    double dv=0, dvx=0, dvy=0;
    double x=0, y=0;
    double headwind=HeadWind(WindSpeed, WindAngle);
    double crosswind=CrossWind(WindSpeed, WindAngle);
    double Gy=GRAVITY*cos(DegtoRad((ShootingAngle + ZAngle)));
    double Gx=GRAVITY*sin(DegtoRad((ShootingAngle + ZAngle)));
    vx=Vi*cos(DegtoRad(ZAngle));
    vy=Vi*sin(DegtoRad(ZAngle));
    y=-SightHeight/12;
    int n=0;
    for (t=0;;t=t+dt){
        vx1=vx, vy1=vy; 
        v=pow(pow(vx,2)+pow(vy,2),0.5);
        dt=0.5/v;

        dv = retard(DragFunction,DragCoefficient,v+headwind);       
        dvx = -(vx/v)*dv;
        dvy = -(vy/v)*dv;

        vx=vx + dt*dvx + dt*Gx;
        vy=vy + dt*dvy + dt*Gy;

        if (x/3>=n){
            ptr[10*n+0]=x/3;                            
            ptr[10*n+1]=y*12;                           
            ptr[10*n+2]=-RadtoMOA(atan(y/x));           
            ptr[10*n+3]=t+dt;                           
            ptr[10*n+4]=Windage(crosswind,Vi,x,t+dt);   
            ptr[10*n+5]=RadtoMOA(atan(ptr[10*n+4]));    
            ptr[10*n+6]=v;                              
            ptr[10*n+7]=vx;                         
            ptr[10*n+8]=vy;                     
            ptr[10*n+9]=0;                              
            n++;    
        }   
        // Compute position based on average velocity.
        x=x+dt*(vx+vx1)/2;
        y=y+dt*(vy+vy1)/2;
        if (fabs(vy)>fabs(3*vx)) break;
        if (n>=__BCOMP_MAXRANGE__+1) break;
    }
    ptr[10*__BCOMP_MAXRANGE__+1]=(double)n;
    *Solution = ptr;
    return n;
}
type
   TDoubleArray  = array[Word] of Double;
   PDoubleArray  = ^TDoubleArray;

ptr: PDoubleArray;
GetMem(ptr, 10*sizeof(double)+324);

ptr[10*n+1] := y*12;   
ptr[1] := n;

编辑完整代码后添加:

argument definition:  
    ... var Solution: PDoubleArray)
in this case usage:   
    Solution := ptr;
type
 DoubleArray = Array[Word] of Double;
 PDoubleArray = ^DoubleArray;
const
 __BCOMP_MAXRANGE__ = 5; // for Example
var
 Ptr : ^DoubleArray;
 Solution : ^PDoubleArray;
 Y : Double;
 N : Integer;
begin
 Ptr := AllocMem(10*SizeOf(Double)+324); // Initialize with Zero
 // OR
 GetMem(Ptr, 10*SizeOf(Double)+324); // Without Initializing
 ...
 Ptr^[10*N+1] := Y*12;
 ...
 Ptr^[10*__BCOMP_MAXRANGE__ + 1] := N;
 ...
 Solution := @Ptr;
 ShowMessage(FloatToStr(Solution^^[1])); // Example for using 'Solution'
 ...
 // Free Memory using FreeMem(Ptr) at the End
end;