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 f6c1d96 commit 5cd7acf
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/tools/tc_utils/tc_gen/tc_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2412,10 +2412,9 @@ void write_nc(GenCTCInfo &gci) {
unixtime valid_end = (unixtime) 0;

// Allocate memory
float *data = (float *) nullptr;
int nx = gci.NcOutGrid->nx();
int ny = gci.NcOutGrid->ny();
data = new float [nx*ny];
vector<float> data(nx*ny, 0.0);

// Loop over vector of output types
for(i=0; i<n_ncout; i++) {
Expand Down Expand Up @@ -2523,9 +2522,6 @@ void write_nc(GenCTCInfo &gci) {
add_att(&nc_var, "valid_beg", unix_to_yyyymmdd_hhmmss(valid_beg));
add_att(&nc_var, "valid_end", unix_to_yyyymmdd_hhmmss(valid_end));

// Reset memory
memset(data, 0, nx*ny);

// Store the data
for(x=0; x<nx; x++) {
for(y=0; y<ny; y++) {
Expand All @@ -2535,17 +2531,14 @@ void write_nc(GenCTCInfo &gci) {
} // end for x

// Write out the data
if(!put_nc_data_with_dims(&nc_var, &data[0], ny, nx)) {
if(!put_nc_data_with_dims(&nc_var, data.data(), ny, nx)) {
mlog << Error << "\nwrite_nc() -> "
<< "error writing NetCDF variable name " << var_name
<< "\n\n";
exit(1);
}
}

// Deallocate and clean up
if(data) { delete [] data; data = (float *) nullptr; }

return;
}

Expand Down

0 comments on commit 5cd7acf

Please sign in to comment.