KaustubhKhati
KaustubhKhati

Reputation: 139

Memory Alloc in C to Java

I Have a certain C program Now I want to Port it into a Java Program. I got most of the things but I have a code like

    tsum=(float *) malloc(cr_nos*sizeof(float));
    rsum=(float *) malloc(cr_nos*sizeof(float));
    ex_sum=(float *) malloc(cr_nos*sizeof(float));
    bsum=(float *) malloc(cr_nos*sizeof(float));

Now How Can I allocate this size in a Java Program? cr_nos is an Integer of size (1-10).

Upvotes: 1

Views: 432

Answers (2)

Krease
Krease

Reputation: 16215

Java and C are very different when it comes to memory management. You don't need to specifically allocate or deallocate memory - just create the objects you need and let Java do the work for you. I suspect there'll be a lot of boilerplate in C that will simply disappear when you are done.

Upvotes: 2

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95569

The Java equivalent of the code above is:

float[] tSum = new float[crCount];
float[] rSum = new float[crCount];
float[] exSum = new float[crCount];
float[] bSum = new float[crCount];

... although, in Java, "t", "r", "ex", "b", and "cr" would typically be unabbreviated.

Upvotes: 4

Related Questions