Skip to content

Commit

Permalink
673 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 f3379fe commit ced8aae
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 316 deletions.
16 changes: 6 additions & 10 deletions src/libcode/vx_data2d_nc_cf/nc_cf_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ bool NcCfFile::open(const char * filepath)
int n_times = get_data_size(valid_time_var);
int tim_buf_size = n_times;
if (use_bounds_var) tim_buf_size *= 2;
auto time_values = new double[tim_buf_size];
vector<double> time_values(tim_buf_size);

if( get_nc_data(nc_time_var, time_values) ) {
if( get_nc_data(nc_time_var, time_values.data()) ) {
bool no_leap_year = get_att_no_leap_year(valid_time_var);
if( time_dim_count > 1 ) {
double latest_time = bad_data_double;
Expand Down Expand Up @@ -381,7 +381,6 @@ bool NcCfFile::open(const char * filepath)
}
}
else ValidTime.add(0); //Initialize
delete [] time_values;
}

NcVar init_time_var = get_var(_ncFile, "forecast_reference_time");
Expand Down Expand Up @@ -504,14 +503,13 @@ bool NcCfFile::open(const char * filepath)
if (IS_VALID_NC_P(z_var)) {

int z_count = get_data_size(z_var);
auto z_values = new double[z_count];
vector<double> z_values(z_count);

if( get_nc_data(z_var, z_values) ) {
if( get_nc_data(z_var, z_values.data()) ) {
for(int i=0; i<z_count; i++) {
vlevels.add(z_values[i]);
}
}
delete [] z_values;
}

// done
Expand Down Expand Up @@ -1102,7 +1100,7 @@ bool NcCfFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const

// get the data
const int plane_size = nx * ny;
double *d = new double[plane_size];
vector<double> d(plane_size);

size_t dim_size;
LongArray offsets;
Expand All @@ -1125,7 +1123,7 @@ bool NcCfFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const
offsets[y_slot] = 0;
lengths[y_slot] = ny;

get_nc_data(v, d, lengths, offsets);
get_nc_data(v, d.data(), lengths, offsets);

int offset = 0;
if( x_slot > y_slot ) {
Expand Down Expand Up @@ -1163,8 +1161,6 @@ bool NcCfFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const
} /* for x */
}

delete [] d;

// done
mlog << Debug(6) << method_name << "took "
<< get_exe_duration(start_clock) << " seconds\n";
Expand Down
5 changes: 2 additions & 3 deletions src/libcode/vx_data2d_nc_met/get_met_grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,9 @@ void get_semilatlon_var(NcFile *ncfile, const char * var_name, NumArray &out_na)

// Store the requested data in the specified NumArray object
long count = get_data_size(&nc_var);
double * data_values = new double[ count ];
get_nc_data(&nc_var, data_values);
vector<double> data_values(count);
get_nc_data(&nc_var, data_values.data());
for(int i=0; i<count; i++) out_na.add(data_values[i]);
if(data_values) { delete [] data_values; data_values = (double *) nullptr; }

return;
}
Expand Down
15 changes: 6 additions & 9 deletions src/libcode/vx_data2d_nc_met/met_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -587,34 +587,31 @@ plane.set_size(Nx, Ny);
dim[x_slot] = Nx;
dim[y_slot] = Ny;

double *data_array = new double[cell_count];
double *double_array = new double[cell_count];
vector<double> data_array(cell_count);
vector<double> double_array(cell_count);

clock_time = clock();

get_nc_data(v, double_array, dim, cur);
copy_nc_data_as_double(data_array, double_array, x_slot, y_slot, Nx, Ny,
get_nc_data(v, double_array.data(), dim, cur);
copy_nc_data_as_double(data_array.data(), double_array.data(), x_slot, y_slot, Nx, Ny,
missing_value, fill_value);

nc_time = clock();
if (mlog.verbosity_level() >= 7) {
double duration_sec = (double)(nc_time - clock_time)/CLOCKS_PER_SEC;
check_nc_data_2d(data_array, Nx, Ny, missing_value);
check_nc_data_2d(data_array.data(), Nx, Ny, missing_value);
mlog << Debug(7) << method_name_short << "took " << duration_sec
<< " seconds to read NetCDF data\n";
}

plane.set_block(data_array, Nx, Ny);
plane.set_block(data_array.data(), Nx, Ny);

if (mlog.verbosity_level() >= 7) {
double duration_sec = (double)(clock() - nc_time)/CLOCKS_PER_SEC;
mlog << Debug(7) << method_name_short << "took " << duration_sec
<< " seconds to fill data plane\n";
}

if (data_array) delete[] data_array;
if (double_array) delete[] double_array;

//
// done
//
Expand Down
4 changes: 2 additions & 2 deletions src/libcode/vx_data2d_nc_wrf/wrf_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ plane.set_size(Nx, Ny);
//
// get the data
//
double d[Ny];
vector<double> d(Ny);

LongArray offsets;
LongArray lengths;
Expand All @@ -863,7 +863,7 @@ lengths[y_slot] = Ny;
int type_id = GET_NC_TYPE_ID_P(v);
for (x=0; x<Nx; ++x) {
offsets[x_slot] = x;
get_nc_data(v, (double *)&d, lengths, offsets);
get_nc_data(v, d.data(), lengths, offsets);

b[x_slot] = x;

Expand Down
29 changes: 11 additions & 18 deletions src/libcode/vx_data2d_ugrid/ugrid_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ bool UGridFile::open_metadata(const char * filepath)
int n_times = IS_VALID_NC_P(_tDim) ? get_dim_size(_tDim)
: (int) get_data_size(valid_time_var);
int tim_buf_size = n_times;
double *time_values = new double[tim_buf_size];
vector<double> time_values(tim_buf_size);
if(2 == time_dim_count) {
for(int i=0; i<n_times; i++) {
time_values[i] = get_nc_time(valid_time_var, i);
Expand All @@ -362,7 +362,7 @@ bool UGridFile::open_metadata(const char * filepath)
<< GET_NC_NAME_P(valid_time_var) << "\n";
}
}
else if( get_nc_data(valid_time_var, time_values) ) {
else if( get_nc_data(valid_time_var, time_values.data()) ) {
bool no_leap_year = get_att_no_leap_year(valid_time_var);
if( time_dim_count > 1 ) {
double latest_time = bad_data_double;
Expand Down Expand Up @@ -396,7 +396,6 @@ bool UGridFile::open_metadata(const char * filepath)
}
}
else ValidTime.add(0); //Initialize
delete [] time_values;
}

// Pull out the grid. This must be done after pulling out the dimension
Expand Down Expand Up @@ -440,14 +439,13 @@ bool UGridFile::open_metadata(const char * filepath)
if (IS_VALID_NC_P(z_var)) {

int z_count = (int) get_data_size(z_var);
double *z_values = new double[z_count];
vector<double> z_values(z_count);

if( get_nc_data(z_var, z_values) ) {
if( get_nc_data(z_var, z_values.data()) ) {
for(int i=0; i<z_count; i++) {
vlevels.add(z_values[i]);
}
}
delete [] z_values;
}

// done
Expand Down Expand Up @@ -699,7 +697,7 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const

// get the data
const int plane_size = nx * ny;
double *d = new double[plane_size];
vector<double> d(plane_size);

int length;
size_t dim_size;
Expand All @@ -726,7 +724,7 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const
}
}

get_nc_data(v, d, lengths, offsets);
get_nc_data(v, d.data(), lengths, offsets);

double min_value = 10e10;
double max_value = -min_value;
Expand All @@ -744,8 +742,6 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const

} // for x

if (nullptr != d) delete [] d;

// done
ConcatString log_message;
for (int idx=0; idx<a.n_elements(); idx++) {
Expand Down Expand Up @@ -906,14 +902,14 @@ void UGridFile::read_netcdf_grid()
ConcatString units_value;
const char *method_name = "UGridFile::read_netcdf_grid() -> ";

double *_lat = new double[face_count];
double *_lon = new double[face_count];
vector<double> _lat(face_count);
vector<double> _lon(face_count);

if (IS_INVALID_NC_P(_latVar)) {
mlog << Error << "\n" << method_name << "latitude variable is missing\n\n";
exit(1);
}
else if (!get_nc_data(_latVar,_lat)) {
else if (!get_nc_data(_latVar,_lat.data())) {
mlog << Error << "\n" << method_name << "fail to read latitude values\n\n";
exit(1);
}
Expand All @@ -922,7 +918,7 @@ void UGridFile::read_netcdf_grid()
mlog << Error << "\n" << method_name << "longitude variable is missing\n\n";
exit(1);
}
else if (!get_nc_data(_lonVar,_lon)) {
else if (!get_nc_data(_lonVar,_lon.data())) {
mlog << Error << "\n" << method_name << "fail to read latitude values\n\n";
exit(1);
}
Expand All @@ -943,17 +939,14 @@ void UGridFile::read_netcdf_grid()
// Convert longitude from degrees east to west
for (int idx=0; idx<face_count; idx++) _lon[idx] = -1.0*rescale_deg(_lon[idx], -180, 180);

grid_data.set_points(face_count, _lon, _lat);
grid_data.set_points(face_count, _lon.data(), _lat.data());
grid_data.max_distance_km = max_distance_km;

grid.set(grid_data);

// Pull the grid projection from the variable information. First, look for
// a grid_mapping attribute.

if (_lat) delete [] _lat;
if (_lon) delete [] _lon;

}


Expand Down
27 changes: 10 additions & 17 deletions src/libcode/vx_nc_util/write_netcdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ void write_netcdf_latlon_1d(NcFile *f_out, NcDim *lat_dim, NcDim *lon_dim,
NcVar lat_var;
NcVar lon_var;
// Allocate space for lat/lon values
float *lat_data = new float [grid.ny()];
float *lon_data = new float [grid.nx()];
vector<float> lat_data(grid.ny());
vector<float> lon_data(grid.nx());

// Define Variables
lat_var = f_out->addVar("lat", ncFloat, *lat_dim);
Expand Down Expand Up @@ -142,13 +142,10 @@ void write_netcdf_latlon_1d(NcFile *f_out, NcDim *lat_dim, NcDim *lon_dim,
}

// Write the lat data
put_nc_data(&lat_var, &lat_data[0], lat_dim->getSize(), 0);
put_nc_data(&lat_var, lat_data.data(), lat_dim->getSize(), 0);

// Write the lon data
put_nc_data(&lon_var, &lon_data[0], lon_dim->getSize(), 0);

if ( lat_data ) { delete [] lat_data; lat_data = nullptr; }
if ( lon_data ) { delete [] lon_data; lon_data = nullptr; }
put_nc_data(&lon_var, lon_data.data(), lon_dim->getSize(), 0);

return;
}
Expand All @@ -164,8 +161,8 @@ void write_netcdf_latlon_2d(NcFile *f_out, NcDim *lat_dim, NcDim *lon_dim,
long counts[2] = {grid.ny(), grid.nx()};
long offsets[2] = {0 , 0};
// Allocate space for lat/lon values
float *lat_data = new float [grid.nx()*grid.ny()];
float *lon_data = new float [grid.nx()*grid.ny()];
vector<float> lat_data(grid.nx()*grid.ny());
vector<float> lon_data(grid.nx()*grid.ny());

// Define Variables
dims.push_back(*lat_dim);
Expand Down Expand Up @@ -196,13 +193,10 @@ void write_netcdf_latlon_2d(NcFile *f_out, NcDim *lat_dim, NcDim *lon_dim,
}

// Write the lat data
put_nc_data(&lat_var, &lat_data[0], counts, offsets);
put_nc_data(&lat_var, lat_data.data(), counts, offsets);

// Write the lon data
put_nc_data(&lon_var, &lon_data[0], counts, offsets);

if ( lat_data ) { delete [] lat_data; lat_data = nullptr; }
if ( lon_data ) { delete [] lon_data; lon_data = nullptr; }
put_nc_data(&lon_var, lon_data.data(), counts, offsets);

return;
}
Expand All @@ -216,7 +210,7 @@ void write_netcdf_grid_weight(NcFile *f_out, NcDim *lat_dim, NcDim *lon_dim,
vector<NcDim> dims;
vector<size_t> count;
// Allocate space for weight values
float *wgt_data = new float [wgt_dp.nx()*wgt_dp.ny()];
vector<float> wgt_data(wgt_dp.nx()*wgt_dp.ny());

// Define Variables
dims.push_back(*lat_dim);
Expand Down Expand Up @@ -256,10 +250,9 @@ void write_netcdf_grid_weight(NcFile *f_out, NcDim *lat_dim, NcDim *lon_dim,
// Write the weights
count.push_back(wgt_dp.ny());
count.push_back(wgt_dp.nx());
put_nc_data_with_dims(&wgt_var, &wgt_data[0], wgt_dp.ny(), wgt_dp.nx());
put_nc_data_with_dims(&wgt_var, wgt_data.data(), wgt_dp.ny(), wgt_dp.nx());

// Clean up
if(wgt_data) { delete [] wgt_data; wgt_data = (float *) nullptr; }

return;
}
Expand Down
Loading

0 comments on commit ced8aae

Please sign in to comment.