Skip to content

Commit

Permalink
Per #2402, update the CRC_Array template class to use stl vectors ins…
Browse files Browse the repository at this point in the history
…tead of dyanmically allocating memory to avoid SonarQube findings.
  • Loading branch information
JohnHalleyGotway committed Feb 16, 2023
1 parent 4e36503 commit 909a6b2
Showing 1 changed file with 11 additions and 64 deletions.
75 changes: 11 additions & 64 deletions src/basic/vx_util/crc_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


#include <iostream>
#include <vector>

#include "num_array.h"
#include "int_array.h"
Expand All @@ -34,27 +35,6 @@
static const int crc_array_alloc_inc = 25;


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


template <typename T>

int is_bigger(const void * p1, const void * p2)

{

const T & a = *((T *) p1);
const T & b = *((T *) p2);

if ( a < b ) return ( -1 );
if ( a > b ) return ( 1 );

return ( 0 );

}



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


Expand All @@ -69,7 +49,7 @@ class CRC_Array {
void assign(const CRC_Array &);


T * e;
std::vector<T> e;

int Nelements;

Expand Down Expand Up @@ -144,12 +124,6 @@ class CRC_Array {
};


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


// static int is_bigger(const void *, const void *);


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


Expand Down Expand Up @@ -205,8 +179,6 @@ void CRC_Array<T>::init_from_scratch()

{

e = (T *) 0;

clear();

return;
Expand All @@ -223,7 +195,7 @@ void CRC_Array<T>::clear()

{

if ( e ) { delete [] e; e = (T *) 0; }
e.clear();

Nelements = Nalloc = 0;

Expand Down Expand Up @@ -286,36 +258,7 @@ if ( ! exact ) {

}

T * u = (T *) 0;

u = new T [len];

if ( !u ) {

mlog << Error << "\nvoid CRC_Array::extend(int, bool) -> "
<< "memory allocation error\n\n";

exit ( 1 );

}

int j;

memset(u, 0, len*sizeof(T));

if ( e ) {

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

u[j] = e[j];

}

delete [] e; e = (T *) 0;

}

e = u; u = (T *) 0;
e.reserve( len );

Nalloc = len;

Expand Down Expand Up @@ -533,7 +476,9 @@ void CRC_Array<T>::add(const T & k)

extend(Nelements + 1, false);

e[Nelements++] = k;
e.push_back(k);

Nelements++;

return;

Expand All @@ -555,7 +500,9 @@ int j;

for (j=0; j<(a.Nelements); ++j) {

e[Nelements++] = a.e[j];
e.push_back(a.e[j]);

Nelements++;

}

Expand Down Expand Up @@ -604,7 +551,7 @@ void CRC_Array<T>::sort_increasing()

if ( Nelements <= 1 ) return;

qsort(e, Nelements, sizeof(*e), is_bigger<T>);
std::sort(e.begin(), e.end());

return;

Expand Down

0 comments on commit 909a6b2

Please sign in to comment.