Commit ffff7f54 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Replace variable length arrays by dynamic memory allocation.

parent d808be4c
......@@ -90,7 +90,7 @@ static
void quick_sort_of_subarray_by_lat(double * array, int subarray_start, int subarray_end)
{
int subarray_length = (subarray_end - subarray_start) / 2 + 1;
double subarray[subarray_length];
double *subarray = (double*) Malloc(subarray_length*sizeof(double));
int subarray_index = 0;
for(int index = subarray_start + 1; index <= subarray_end + 1; index += 2){
......@@ -105,7 +105,9 @@ void quick_sort_of_subarray_by_lat(double * array, int subarray_start, int subar
for(int index = subarray_start + 1; index <= subarray_end + 1; index += 2){
array[index] = subarray[subarray_index];
subarray_index += 1;
}
}
Free(subarray);
}
......@@ -311,7 +313,7 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
*/
double center_point_xyz[3];
double cell_corners_xyz_open_cell[ncorner * 3];
double *cell_corners_xyz_open_cell = (double*) Malloc(3*ncorner*sizeof(double));
double corner_coordinates[3];
double second_corner_coordinates[3];
......@@ -329,7 +331,7 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
double *p_surface_normal_of_the_cell = &surface_normal_of_the_cell[0];
int no_cells_with_a_specific_no_of_corners[ncorner];
int *no_cells_with_a_specific_no_of_corners = (int*) Malloc(ncorner*sizeof(int));
for ( int i = 0; i < ncorner; i++ )
no_cells_with_a_specific_no_of_corners[i] = 0;
......@@ -455,7 +457,7 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
/* Checks if there are any duplicate vertices in the list of corners. Note that the last (additional) corner has not been set yet. */
int marked_duplicate_indices[actual_number_of_corners];
int *marked_duplicate_indices = (int*) Malloc(actual_number_of_corners*sizeof(int));
for ( int i = 0; i < actual_number_of_corners; i++ ) marked_duplicate_indices[i] = 0;
int no_duplicates = 0;
......@@ -476,7 +478,7 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
/* Writes the unique corner vertices in a new array. */
double cell_corners_xyz_without_duplicates[(actual_number_of_corners - no_duplicates) * 3];
double *cell_corners_xyz_without_duplicates = (double*) Malloc(3*(actual_number_of_corners - no_duplicates)*sizeof(double));
int unique_corner_number = 0;
......@@ -509,7 +511,7 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
/* We are creating a closed polygon/cell by setting the additional last corner to be the same as the first one. */
double cell_corners_xyz[(actual_number_of_corners + 1) * 3];
double *cell_corners_xyz = (double*) Malloc(3*(actual_number_of_corners + 1)*sizeof(double));
for ( int corner_no = 0; corner_no < actual_number_of_corners; corner_no++ )
{
......@@ -555,7 +557,7 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
/* The remaining two-dimensional coordinates are extracted into one array for all the cell's corners and into one array for the center point. */
double cell_corners_plane_projection[(actual_number_of_corners +1) * 2];
double *cell_corners_plane_projection = (double*) Malloc(2*(actual_number_of_corners +1)*sizeof(double));
/* The following projection on the plane that two coordinate axes lie on changes the arrangement of the polygon vertices if the coordinate to be ignored along the third axis is smaller than 0.
In this case, the result of the computation of the orientation of vertices needs to be inverted. Clockwise becomes counterclockwise and vice versa. */
......@@ -630,6 +632,11 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
printf(" %g/%g ", grid_corner_lon[cell_no * ncorner + corner_no], grid_corner_lat[cell_no * ncorner + corner_no]);
printf("\n");
}
Free(cell_corners_plane_projection);
Free(cell_corners_xyz);
Free(cell_corners_xyz_without_duplicates);
Free(marked_duplicate_indices);
}
int no_nonunique_cells = gridsize - no_unique_center_points;
......@@ -655,6 +662,9 @@ void verify_grid(int gridtype, int gridsize, int gridno, int ngrids, int ncorner
cdoPrintRed("%9d cells have their center points located outside their boundaries", no_of_cells_with_center_points_out_of_bounds);
cdoPrint("");
Free(no_cells_with_a_specific_no_of_corners);
Free(cell_corners_xyz_open_cell);
}
......
......@@ -75,9 +75,9 @@ void *Vertcum(void *argument)
int vlistID2 = vlistDuplicate(vlistID1);
int nvars = vlistNvars(vlistID1);
int *varnmiss[nvars];
double **vardata1[nvars];
double **vardata2[nvars];
int **varnmiss = (int**) Malloc(nvars*sizeof(int*));
double ***vardata1 = (double***) Malloc(nvars*sizeof(double**));
double ***vardata2 = (double***) Malloc(nvars*sizeof(double**));
int zaxisIDhl = -1;
......@@ -105,9 +105,10 @@ void *Vertcum(void *argument)
zaxisInqVct(zaxisID, vct);
zaxisIDhl = zaxisCreate(ZAXIS_HYBRID_HALF, nlevshl);
double levels[nlevshl];
double *levels = (double*) Malloc(nlevshl*sizeof(double));
for ( levelID = 0; levelID < nlevshl; ++levelID ) levels[levelID] = levelID+1;
zaxisDefLevels(zaxisIDhl, levels);
Free(levels);
zaxisDefVct(zaxisIDhl, nvct, vct);
vlistChangeZaxisIndex(vlistID2, i, zaxisIDhl);
}
......@@ -230,6 +231,10 @@ void *Vertcum(void *argument)
Free(varnmiss[varID]);
}
Free(vardata1);
Free(vardata2);
Free(varnmiss);
streamClose(streamID2);
streamClose(streamID1);
......
......@@ -221,11 +221,11 @@ void *Vertintap(void *argument)
}
}
bool vars[nvars];
double *vardata1[nvars];
double *vardata2[nvars];
int *varnmiss[nvars];
bool varinterp[nvars];
bool *vars = (bool*) Malloc(nvars*sizeof(bool));
bool *varinterp = (bool*) Malloc(nvars*sizeof(bool));
double **vardata1 = (double**) Malloc(nvars*sizeof(double*));
double **vardata2 = (double**) Malloc(nvars*sizeof(double*));
int **varnmiss = (int**) Malloc(nvars*sizeof(int*));
int maxlev = nhlevh > nplev ? nhlevh : nplev;
......@@ -237,7 +237,7 @@ void *Vertintap(void *argument)
{
int nlev = zaxisInqSize(zaxisIDh);
if ( nlev != nhlev ) cdoAbort("Internal error, wrong number of height level!");
double levels[nlev];
double *levels = (double*) Malloc(nlev*sizeof(double));
zaxisInqLevels(zaxisIDh, levels);
for ( int ilev = 0; ilev < nlev; ++ilev )
......@@ -248,6 +248,8 @@ void *Vertintap(void *argument)
break;
}
}
Free(levels);
}
if ( zaxisIDh != -1 && gridsize > 0 )
......@@ -262,7 +264,7 @@ void *Vertintap(void *argument)
if ( operfunc == func_hl )
{
double phlev[nplev];
double *phlev =(double*) Malloc(nplev*sizeof(double));
height2pressure(phlev, plev, nplev);
if ( cdoVerbose )
......@@ -270,6 +272,7 @@ void *Vertintap(void *argument)
cdoPrint("level = %d height = %g pressure = %g", i+1, plev[i], phlev[i]);
memcpy(plev, phlev, nplev*sizeof(double));
Free(phlev);
}
if ( opertype == type_log )
......@@ -470,6 +473,12 @@ void *Vertintap(void *argument)
if ( dpress ) Free(dpress);
if ( vct ) Free(vct);
Free(vars);
Free(varinterp);
Free(vardata1);
Free(vardata2);
Free(varnmiss);
lista_destroy(flista);
cdoFinish();
......
......@@ -212,7 +212,7 @@ void *Vertstat(void *argument)
int nzaxis = vlistNzaxis(vlistID1);
int nlev, zaxisID;
vert_t vert[nzaxis];
vert_t *vert = (vert_t*) Malloc(nzaxis*sizeof(vert_t));
if ( needWeights )
{
bool genbounds = false;
......@@ -461,6 +461,8 @@ void *Vertstat(void *argument)
for ( int index = 0; index < nzaxis; ++index )
if ( vert[index].numlevel > 1 ) Free(vert[index].weights);
Free(vert);
streamClose(streamID2);
streamClose(streamID1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment