Skip to content

Commit

Permalink
Per #2402, rollback changes to set.h and set.cc which seem to have ca…
Browse files Browse the repository at this point in the history
…used a runtime memory error in MODE.
  • Loading branch information
JohnHalleyGotway committed Feb 16, 2023
1 parent 8f2445a commit 1a5b076
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
78 changes: 56 additions & 22 deletions src/libcode/vx_shapedata/set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ void FcstObsSet::init_from_scratch()

{

fcst_number = 0;

obs_number = 0;

all_clear();

extend_fcst (50);
Expand All @@ -111,13 +115,18 @@ void FcstObsSet::clear()

{

// if ( fcst_number ) { delete [] fcst_number; fcst_number = 0; }
// if ( obs_number ) { delete [] obs_number; obs_number = 0; }

int j;

for (j=0; j<n_fcst_alloc; ++j) fcst_number[j] = 0;
for (j=0; j<n_obs_alloc; ++j) obs_number[j] = 0;

n_fcst = n_obs = 0;

// n_fcst_alloc = n_obs_alloc = 0;

return;

}
Expand All @@ -130,8 +139,8 @@ void FcstObsSet::all_clear()

{

fcst_number.clear();
obs_number.clear();
if ( fcst_number ) { delete [] fcst_number; fcst_number = 0; }
if ( obs_number ) { delete [] obs_number; obs_number = 0; }

n_fcst = n_obs = 0;

Expand All @@ -156,7 +165,7 @@ if ( s.n_fcst_alloc > 0 ) {

extend_fcst (s.n_fcst_alloc);

fcst_number = s.fcst_number;
memcpy(fcst_number, s.fcst_number, (s.n_fcst_alloc)*sizeof(int));

}

Expand All @@ -165,7 +174,7 @@ if ( s.n_obs_alloc > 0 ) {

extend_obs (s.n_obs_alloc);

obs_number = s.obs_number;
memcpy(obs_number, s.obs_number, (s.n_obs_alloc)*sizeof(int));

}

Expand Down Expand Up @@ -211,19 +220,30 @@ return;
///////////////////////////////////////////////////////////////////////////////


void FcstObsSet::extend(std::vector<int> & a, int & n_alloc, const int N)
void FcstObsSet::extend(int * & a, int & n_alloc, const int N)

{

if ( N <= n_alloc ) return;

int k = N/fcst_obs_set_alloc_inc;
int j, k;
int * u = 0;

k = N/fcst_obs_set_alloc_inc;

if ( N%fcst_obs_set_alloc_inc ) ++k;

k *= fcst_obs_set_alloc_inc;

a.reserve(k);
u = new int [k];

if ( a ) memcpy(u, a, n_alloc*sizeof(int));

for (j=n_alloc; j<k; ++j) u[j] = 0;

if ( a ) { delete [] a; a = 0; }

a = u; u = 0;

n_alloc = k;

Expand Down Expand Up @@ -287,9 +307,7 @@ void FcstObsSet::add_fcst(int k) {

extend_fcst(n_fcst + 1);

fcst_number.push_back(k);

n_fcst++;
fcst_number[n_fcst++] = k;

return;
}
Expand All @@ -304,9 +322,7 @@ void FcstObsSet::add_obs(int k) {

extend_obs(n_obs + 1);

obs_number.push_back(k);

n_obs++;
obs_number[n_obs++] = k;

return;

Expand Down Expand Up @@ -382,6 +398,8 @@ void SetCollection::init_from_scratch()

{

set = 0;

all_clear();

extend(10);
Expand All @@ -398,12 +416,17 @@ void SetCollection::clear()

{

// if ( set ) { delete [] set; set = 0; }

n_sets = 0;

int j;

for (j=0; j<n_alloc; ++j) set[j].clear();

// n_alloc = 0;


return;

}
Expand All @@ -416,7 +439,7 @@ void SetCollection::all_clear()

{

set.clear();
if ( set ) { delete [] set; set = 0; }

n_sets = 0;

Expand All @@ -437,7 +460,7 @@ void SetCollection::assign(const SetCollection & s)

all_clear();

if ( s.set.size() == 0 ) return;
if ( ! (s.set) ) return;

extend(s.n_alloc);

Expand All @@ -462,13 +485,26 @@ void SetCollection::extend(int N)

if ( N <= n_alloc ) return;

int k = N/fcst_obs_set_alloc_inc;
int j, k;
FcstObsSet * u = 0;

k = N/fcst_obs_set_alloc_inc;

if ( N%fcst_obs_set_alloc_inc ) ++k;

k *= fcst_obs_set_alloc_inc;

set.reserve(k);
u = new FcstObsSet [k];

if ( set ) {

for (j=0; j<n_alloc; ++j) u[j] = set[j];

delete [] set; set = 0;

}

set = u; u = 0;

n_alloc = k;

Expand All @@ -493,13 +529,11 @@ void SetCollection::add_pair(int fcst, int obs)

extend(n_sets + 1);

FcstObsSet s;

s.add_pair(fcst, obs);
set[n_sets].clear();

set.push_back(s);
set[n_sets].add_pair(fcst, obs);

n_sets++;
++n_sets;

do {

Expand Down
16 changes: 6 additions & 10 deletions src/libcode/vx_shapedata/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
///////////////////////////////////////////////////////////////////////////////


#include <vector>


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


static const int fcst_obs_set_alloc_inc = 50;


Expand All @@ -46,16 +40,16 @@ class FcstObsSet {

void assign(const FcstObsSet &);

void extend(std::vector<int> &, int & n_alloc, const int n_new);
void extend(int * &, int & n_alloc, const int n_new);

public:

//
// data
//

std::vector<int> fcst_number;
std::vector<int> obs_number;
int * fcst_number; // allocated
int * obs_number; // allocated

int n_fcst;
int n_obs;
Expand Down Expand Up @@ -102,6 +96,8 @@ extern std::ostream & operator<<(std::ostream &, const FcstObsSet &);
///////////////////////////////////////////////////////////////////////////////


// static const int max_fcst_obs_sets = 300;

static const int set_alloc_inc = 50;


Expand All @@ -122,7 +118,7 @@ class SetCollection {
// data
//

std::vector<FcstObsSet> set;
FcstObsSet * set; // allocated

int n_sets;

Expand Down

0 comments on commit 1a5b076

Please sign in to comment.