[Solved] Converting C code to Java [closed]


double* ptr;
ptr = (double*)malloc(10*_R_CONST*sizeof(double)+2048);

This is the C way of allocating an array dynamically at runtime, the Java equivalent is

double[] ptr = new double[10*_R_CONST+256];

(Note that Java does not need the sizeof(double) factor, since it allocates objects, not bytes. For the same reason, the 2048 byte buffer shrinks to 256 doubles.)

Similar, the method declaration

double GetRange(double* sln, int yardage)

would translate to

double GetRange(double[] sln, int yardage)

The other lines you pointed out are simply accesses to elements of this array of doubles.

*Solution = ptr;

You didn’t show the declaration of Solution nor asked about it, but my guess is that Solution is a double ** and passed as argument. There is no direct Java equivalent for this (though it can be simulated); what it does is to store the address of the allocated array into a variable provided by the caller of the method.

1

solved Converting C code to Java [closed]