Skip to content

Commit

Permalink
#2673 SonarQube findings: use vector for the dynamic memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard Soh committed Oct 9, 2024
1 parent 8151360 commit 571bdc2
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 238 deletions.
12 changes: 4 additions & 8 deletions src/basic/vx_math/hist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Histogram::~Histogram()

{

if ( Count ) { delete [] Count; Count = (int *) nullptr; }
Count.clear();

}

Expand Down Expand Up @@ -93,7 +93,7 @@ void Histogram::init_from_scratch()

{

Count = (int *) nullptr;
Count.clear();

Nbins = 0;

Expand All @@ -120,9 +120,7 @@ void Histogram::clear()

{

int j;

for (j=0; j<Nbins; ++j) Count[j] = 0;
//for (int j=0; j<Nbins; ++j) Count[j] = 0;

is_empty = 1;

Expand All @@ -142,15 +140,13 @@ void Histogram::set_nbd(int n, double b, double d)

{

if ( Count ) { delete [] Count; Count = (int *) nullptr; }

Nbins = n;

Bottom = b;
Delta = d;


Count = new int [Nbins];
Count.resize(Nbins, 0);

clear();

Expand Down
3 changes: 2 additions & 1 deletion src/basic/vx_math/hist.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////


#include <vector>
#include <iostream>


Expand All @@ -28,7 +29,7 @@ class Histogram {

private:

int * Count;
std::vector<int> Count;

int Nbins;

Expand Down
12 changes: 6 additions & 6 deletions src/basic/vx_math/legendre.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ void Legendre::init_from_scratch()

{

P = 0;
P.clear();

PP = 0;
PP.clear();

clear();

Expand All @@ -135,9 +135,9 @@ void Legendre::clear()

{

if ( P ) { delete [] P; P = nullptr; }
P.clear();

if ( PP ) { delete [] PP; PP = nullptr; }
PP.clear();

X = 0.0;

Expand Down Expand Up @@ -201,9 +201,9 @@ clear();

MaxDegree = N;

P = new double [N + 1];
P.resize(N + 1);

PP = new double [N + 1];
PP.resize(N + 1);

calc(0.0);

Expand Down
8 changes: 6 additions & 2 deletions src/basic/vx_math/legendre.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#define __VX_LEGENDRE_H__


////////////////////////////////////////////////////////////////////////

#include <vector>

////////////////////////////////////////////////////////////////////////


Expand All @@ -37,9 +41,9 @@ class Legendre {

double X; // last x value

double * P; // allocated
std::vector<double> P; // allocated

double * PP; // allocated
std::vector<double> PP; // allocated

public:

Expand Down
4 changes: 1 addition & 3 deletions src/basic/vx_math/ptile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,10 @@ if ( n <= 1 ) return 0;
int i, j, ties_current, ties_total, tie_rank_start = 0, tie_rank_end;
double tie_rank_mean;
RankInfo *rank_info = (RankInfo *) nullptr;
double *ordered_array = (double *) nullptr;
vector<double> ordered_array(n);
double prev_v, v;

rank_info = new RankInfo [n];
ordered_array = new double [n];

// Each RankInfo structure contains a index value from 0 to n-1 and a pointer
// to the data to be ranked
Expand Down Expand Up @@ -296,7 +295,6 @@ if(ties_current != 0) {
}

if(rank_info) { delete [] rank_info; rank_info = (RankInfo *) nullptr; }
if(ordered_array) { delete [] ordered_array; ordered_array = (double *) nullptr; }

return ties_total;

Expand Down
89 changes: 40 additions & 49 deletions src/basic/vx_util/num_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void NumArray::init_from_scratch()
{

clear();

return;

}
Expand Down Expand Up @@ -171,7 +171,7 @@ void NumArray::assign(const NumArray & a)
clear();

e = a.e;

Sorted = a.Sorted;

return;
Expand Down Expand Up @@ -209,7 +209,7 @@ void NumArray::dump(ostream & out, int depth) const
int j;

for (j=0; j<n_elements(); ++j) {

out << prefix << "Element # " << j << " = " << e[j] << "\n";

}
Expand Down Expand Up @@ -285,7 +285,7 @@ int NumArray::has(double d, bool forward) const
}
}
}

return found;

}
Expand Down Expand Up @@ -352,7 +352,7 @@ void NumArray::add(const NumArray & a)
{

extend(n_elements() + a.n_elements());

int j;

for (j=0; j<(a.n_elements()); ++j) {
Expand All @@ -376,7 +376,7 @@ void NumArray::add_const(double v, int n)
{

extend(n_elements() + n);

int j;

for (j=0; j<n; ++j) {
Expand All @@ -400,7 +400,7 @@ void NumArray::add_seq(int beg, int end)
{

extend(n_elements() + (end - beg + 1));

int j;

for (j=beg; j<=end; ++j) {
Expand Down Expand Up @@ -494,7 +494,7 @@ void NumArray::set(double d)
{

erase();

add(d);

//
Expand Down Expand Up @@ -582,7 +582,7 @@ void NumArray::sort_array(bool increasing)
else sort(e.rbegin(), e.rend());

}

Sorted = true;

return;
Expand Down Expand Up @@ -639,29 +639,27 @@ int NumArray::rank_array(int &ties)

{

int n_vld, i;

double *data = (double *) nullptr;
int *data_loc = (int *) nullptr;
double *data_rank = (double *) nullptr;
int i;
int n_vld;
int n = n_elements();

//
// Arrays to store the raw data values to be ranked, their locations,
// and their computed ranks. The ranks are stored as doubles since
// they can be set to 0.5 in the case of ties.
//
data = new double [n_elements()];
data_loc = new int [n_elements()];
data_rank = new double [n_elements()];

if ( !data || !data_loc || !data_rank ) {

mlog << Error << "\nint NumArray::rank_array() -> "
<< "memory allocation error\n\n";

exit ( 1 );
vector<double> data (n);
vector<int > data_loc (n);
vector<double> data_rank (n);

}
//if ( !data || !data_loc || !data_rank ) {
//
// mlog << Error << "\nint NumArray::rank_array() -> "
// << "memory allocation error\n\n";
//
// exit ( 1 );
//
//}

//
// Search the data array for valid data and keep track of its location
Expand All @@ -679,20 +677,13 @@ int NumArray::rank_array(int &ties)
// Compute the rank of the data and store the ranks in the data_rank array
// Keep track of the number of ties in the ranks.
//
ties = do_rank(data, data_rank, n_vld);
ties = do_rank(data.data(), data_rank.data(), n_vld);

//
// Store the data_rank values
//
for(i=0; i<n_vld; i++) e[data_loc[i]] = data_rank[i];

//
// Deallocate memory
//
if(data) { delete [] data; data = (double *) nullptr; }
if(data_loc) { delete [] data_loc; data_loc = (int *) nullptr; }
if(data_rank) { delete [] data_rank; data_rank = (double *) nullptr; }

Sorted = false;

return n_vld;
Expand All @@ -715,7 +706,7 @@ double NumArray::percentile_array(double t)
if ( !Sorted ) sort_array();

v = percentile(e.data(), n_elements(), t);

return v;

}
Expand Down Expand Up @@ -1001,7 +992,7 @@ ConcatString NumArray::serialize() const

s << e[0];
for(j=1; j<n_elements(); j++) s << " " << e[j];

return s;

}
Expand Down Expand Up @@ -1054,7 +1045,7 @@ NumArray NumArray::subset(int beg, int end) const
<< "range check error\n\n";
exit ( 1 );
}

// Store subset
for(int i=beg; i<=end; i++) subset_na.add(e[i]);

Expand All @@ -1081,7 +1072,7 @@ NumArray NumArray::subset(const NumArray &keep) const
<< n_elements() << " array elements)\n\n";
exit ( 1 );
}

// Store subset
for(int i=0; i<=n_elements(); i++) {
if(keep[i]) subset_na.add(e[i]);
Expand All @@ -1101,7 +1092,7 @@ double NumArray::mean() const

int j, count;
double s, mn;

for(j=0, count=0, s=0.0; j<n_elements(); j++) {
if(is_bad_data(e[j])) continue;
s += e[j];
Expand All @@ -1127,7 +1118,7 @@ double NumArray::mean_sqrt() const

// for simple mean, call weighted mean with constant weight
wgt.add_const(1.0, n_elements());

return wmean_sqrt(wgt);

}
Expand All @@ -1144,7 +1135,7 @@ double NumArray::mean_fisher() const

// for simple mean, call weighted mean with constant weight
wgt.add_const(1.0, n_elements());

return wmean_fisher(wgt);

}
Expand Down Expand Up @@ -1300,19 +1291,19 @@ double NumArray::mean_abs_diff() const
double sum, mad;

int n = n_elements();

for(i=0, count=0, sum=0.0; i<n; i++) {
for(j=i+1; j<n; j++) {

if( is_bad_data(e[i]) || is_bad_data(e[j]) ) continue;
sum += abs(e[i]-e[j]);
count++;
}
}

if(count == 0) mad = bad_data_double;
else mad = sum / count;

return mad;

}
Expand All @@ -1326,16 +1317,16 @@ double NumArray::wmean_abs_diff() const
{

double wmad;

int n = n_elements();
double wgt = 1.0/(2.0*n);
double mad = mean_abs_diff();
if( is_bad_data(mad) )

if( is_bad_data(mad) )
wmad = bad_data_double;
else
else
wmad = wgt * mad;

return wmad;

}
Expand Down
Loading

0 comments on commit 571bdc2

Please sign in to comment.