From 9ab5aac40f3c6bd55f87e4c3697d3296c6a356c6 Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 7 May 2024 20:34:40 +0200 Subject: [PATCH] Changed last remaining mallocs to new --- src/HeightMap.cpp | 21 ++++++++++----------- src/contour_creator.hh | 5 +++++ src/main.cpp | 1 - 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/HeightMap.cpp b/src/HeightMap.cpp index f3db3d3..b3d374b 100644 --- a/src/HeightMap.cpp +++ b/src/HeightMap.cpp @@ -6,7 +6,6 @@ #include #include "gdal/gdal_priv.h" #include -//#include "matplotlibcpp.h" #include "contour_creator.hh" @@ -31,13 +30,13 @@ HeightMap::HeightMap(const char* filepath) this->min = band->GetMinimum(); this->max = band-> GetMaximum(); //https://gdal.org/api/gdaldataset_cpp.html#_CPPv4N11GDALDataset15GetGeoTransformEPd - this->geotransform = (double *) CPLMalloc(sizeof(double)*6); + this->geotransform = new double[6]; this->reference_system = *(file->GetSpatialRef()); this->filepath = filepath; file->GetGeoTransform(this->geotransform); - this->data = (float *) CPLMalloc(sizeof(float)*width*height); + this->data = new float[width*height]; CPLErr error = band->RasterIO( GF_Read, 0, 0, width, height, this->data, width, height, GDT_Float32, 0, 0 ); @@ -48,9 +47,7 @@ HeightMap::HeightMap(const char* filepath) float HeightMap::get_pixel(int x, int y) { // all the pixels are in an array of floats from left to right, top to bottom - int offset = ((this->width * y) + x); - //std::cout << " offset: " << offset << " " << x << ","<< y; - return *(this->data + offset); + return this->data[((this->width * y) + x)]; } void HeightMap::blur(float standard_deviation) @@ -59,10 +56,10 @@ void HeightMap::blur(float standard_deviation) int kernel_height = 5; int kernel_width = 5; int kernel_size = kernel_height*kernel_width; - float* kernel = (float*) CPLMalloc(sizeof(float)*kernel_size); + float* kernel = new float [kernel_size]; for (int i=0; iwidth*this->height]; @@ -87,10 +84,12 @@ void HeightMap::blur(float standard_deviation) } blurred_pixel += this->get_pixel(x, y); } - *(blurred + i) = blurred_pixel/float(kernel_size); + blurred[i] = blurred_pixel/float(kernel_size); } } - free(this->data); + + delete[] kernel; + delete[] this->data; this->data = blurred; blurred = nullptr; } @@ -139,7 +138,7 @@ void HeightMap::calculate_steepness() int kernel_width = 5; int kernel_size = kernel_height*kernel_width; - float* kernel = new float[sizeof(float)*kernel_size]; + float* kernel = new float[kernel_size]; float* steepness = new float[this->width*this->height]; #pragma omp parallel { diff --git a/src/contour_creator.hh b/src/contour_creator.hh index 841ec0b..c697b6e 100644 --- a/src/contour_creator.hh +++ b/src/contour_creator.hh @@ -17,6 +17,11 @@ class HeightMap float max; //!< Maximum value in image OGRSpatialReference reference_system; HeightMap(const char* filepath); + ~HeightMap() + { + delete[] data; + delete[] geotransform; + } const char* filepath; float get_pixel(int x,int y); void blur(float standard_deviation); diff --git a/src/main.cpp b/src/main.cpp index c47e716..2185237 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,7 +21,6 @@ std::vector produce_cellmap(HeightMap* heightmap, float z) { int length = (heightmap->width-1)*(heightmap->height-1); // Defining length of vector - uint8_t *cells = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length); std::vector points; // Initiating a vector of points for (int i = 0; iwidth-1);