c++到Delphi的指针转换
C++ to Delphi pointer conversion
我正在尝试将一个开源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;
相关文章:
- 正在将指针转换为范围
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- C++:Lambda 函数指针转换的用例是什么?
- 如何将 int 指针转换为浮点指针
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 将(N 个字节)无符号字符指针转换为浮点数和双 C++
- 我们可以在不知道其真实类型的情况下将基类指针转换为派生类指针吗?
- C++中数组大小未知的指针转换
- Antlr cpp 运行时 任何错误的指针转换?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- C 指针转换会导致内存访问冲突
- 模板类实例化中的指针转换无效
- 函数调用歧义(用户定义的转换和 Derived2Base 指针转换)
- 如何将 void(*)() 类型的指针转换为 void*
- 数组到指针转换期间的临时具体化
- 直接为浮点变量分配十六进制整数与通过指针转换分配之间的区别
- 将基类指针转换为派生的类指针
- 应对 std::字符串中的 std::<char>指针转换后的向量
- 在编译时将函数指针转换为 std::uintptr_t
- 将基类指针转换为未知派生类指针