Skip to content

Commit 1730b8c

Browse files
committed
classify/cluster: Replace Emalloc by std::vector
This should fix a warning from LGTM: Multiplication result may overflow 'int' before it is converted to 'unsigned long'. Signed-off-by: Stefan Weil <[email protected]>
1 parent cbd09de commit 1730b8c

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/classify/cluster.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
** limitations under the License.
1616
*****************************************************************************/
1717

18+
#include <cfloat> // for FLT_MAX
19+
#include <cmath>
20+
#include <vector> // for std::vector
21+
1822
#include "cluster.h"
19-
#include "cutil.h" // for void_proc
23+
#include "cutil.h" // for void_proc
2024
#include "emalloc.h"
2125
#include "genericheap.h"
2226
#include "helpers.h"
2327
#include "kdpair.h"
2428
#include "matrix.h"
2529
#include "tprintf.h"
26-
#include <cfloat> // for FLT_MAX
27-
#include <cmath>
2830

2931
#define HOTELLING 1 // If true use Hotelling's test to decide where to split.
3032
#define FTABLE_X 10 // Size of FTable.
@@ -1084,10 +1086,9 @@ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer,
10841086
int TotalDims = Left->SampleCount + Right->SampleCount;
10851087
if (TotalDims < N + 1 || TotalDims < 2)
10861088
return nullptr;
1087-
const int kMatrixSize = N * N * sizeof(float);
1088-
float *Covariance = static_cast<float *>(Emalloc(kMatrixSize));
1089-
float *Inverse = static_cast<float *>(Emalloc(kMatrixSize));
1090-
float *Delta = static_cast<float *>(Emalloc(N * sizeof(float)));
1089+
std::vector<float> Covariance(N * N);
1090+
std::vector<float> Inverse(N * N);
1091+
std::vector<float> Delta(N);
10911092
// Compute a new covariance matrix that only uses essential features.
10921093
for (int i = 0; i < N; ++i) {
10931094
int row_offset = i * N;
@@ -1107,7 +1108,7 @@ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer,
11071108
}
11081109
}
11091110
}
1110-
double err = InvertMatrix(Covariance, N, Inverse);
1111+
double err = InvertMatrix(&Covariance[0], N, &Inverse[0]);
11111112
if (err > 1) {
11121113
tprintf("Clustering error: Matrix inverse failed with error %g\n", err);
11131114
}
@@ -1129,9 +1130,6 @@ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer,
11291130
}
11301131
Tsq += Delta[x] * temp;
11311132
}
1132-
free(Covariance);
1133-
free(Inverse);
1134-
free(Delta);
11351133
// Changed this function to match the formula in
11361134
// Statistical Methods in Medical Research p 473
11371135
// By Peter Armitage, Geoffrey Berry, J. N. S. Matthews.

0 commit comments

Comments
 (0)