Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a redundant allocation of memory space in a multi dimensional array?

From the Matrix Chain Multiplication page on Wikipedia, there is this fragment of Java code:

public void matrixChainOrder(int[] p) {
    int n = p.length - 1;
    m = new int[n][n];
    s = new int[n][n];
    for (int i = 0; i < n; i++) {
        m[i] = new int[n];
        m[i][i] = 0;
        s[i] = new int[n];
    }
    ...

Isn't m = new int[n][n]; already allocating memory space of size n in both its dimensions so this step in the loop m[i] = new int[n]; is actually redundant because all it does is reallocate the second dimension again?

like image 325
ThisClark Avatar asked Mar 19 '15 05:03

ThisClark


1 Answers

Yes, it is.

m[i] = new int[n]; is absolutely superfluous. And it seems that this line is heritage from c-style psedocode, where such initilization was nessecary.

like image 60
Andremoniy Avatar answered Nov 01 '22 04:11

Andremoniy