Skip to content

Commit

Permalink
STYLE: More make_unique_for_overwrite, less delete[], on local variables
Browse files Browse the repository at this point in the history
Replaced `new T[n]` with `make_unique_for_overwrite<T[]>(n)` calls, for the
initialization of local variables in ITK library files (*.h, *.hxx), and
removed the corresponding `delete[]` statements.

Follow-up to pull request #3590
commit 15d09d6
"STYLE: Replace `new T[n]` with `make_unique_for_overwrite` in cxx files"
  • Loading branch information
N-Dekker authored and hjmjohnson committed Sep 5, 2022
1 parent 68fea25 commit a9bd570
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 139 deletions.
34 changes: 14 additions & 20 deletions Modules/Core/Common/include/itkSymmetricEigenAnalysis.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkSymmetricEigenAnalysis_hxx

#include "itkMath.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand Down Expand Up @@ -56,11 +57,11 @@ template <typename TMatrix, typename TVector, typename TEigenMatrix>
unsigned int
SymmetricEigenAnalysis<TMatrix, TVector, TEigenMatrix>::ComputeEigenValuesLegacy(const TMatrix & A, TVector & D) const
{
auto * workArea1 = new double[m_Dimension];
const auto workArea1 = make_unique_for_overwrite<double[]>(m_Dimension);

// Copy the input matrix
auto * inputMatrix = new double[m_Dimension * m_Dimension];
auto * dVector = new double[m_Dimension];
const auto inputMatrix = make_unique_for_overwrite<double[]>(m_Dimension * m_Dimension);
const auto dVector = make_unique_for_overwrite<double[]>(m_Dimension);

unsigned int k = 0;

Expand All @@ -75,18 +76,14 @@ SymmetricEigenAnalysis<TMatrix, TVector, TEigenMatrix>::ComputeEigenValuesLegacy
}
}

this->ReduceToTridiagonalMatrix(inputMatrix, dVector, workArea1, workArea1);
const unsigned int eigenErrIndex = this->ComputeEigenValuesUsingQL(dVector, workArea1);
this->ReduceToTridiagonalMatrix(inputMatrix.get(), dVector.get(), workArea1.get(), workArea1.get());
const unsigned int eigenErrIndex = this->ComputeEigenValuesUsingQL(dVector.get(), workArea1.get());

for (unsigned int i = 0; i < m_Dimension; ++i)
{
D[i] = dVector[i];
}

delete[] dVector;
delete[] workArea1;
delete[] inputMatrix;

return eigenErrIndex; // index of eigenvalue that could not be computed
}

Expand All @@ -97,12 +94,12 @@ SymmetricEigenAnalysis<TMatrix, TVector, TEigenMatrix>::ComputeEigenValuesAndVec
TVector & EigenValues,
TEigenMatrix & EigenVectors) const
{
auto * workArea1 = new double[m_Dimension];
auto * workArea2 = new double[m_Dimension * m_Dimension];
const auto workArea1 = make_unique_for_overwrite<double[]>(m_Dimension);
const auto workArea2 = make_unique_for_overwrite<double[]>(m_Dimension * m_Dimension);

// Copy the input matrix
auto * inputMatrix = new double[m_Dimension * m_Dimension];
auto * dVector = new double[m_Dimension];
const auto inputMatrix = make_unique_for_overwrite<double[]>(m_Dimension * m_Dimension);
const auto dVector = make_unique_for_overwrite<double[]>(m_Dimension);

unsigned int k = 0;

Expand All @@ -118,9 +115,11 @@ SymmetricEigenAnalysis<TMatrix, TVector, TEigenMatrix>::ComputeEigenValuesAndVec
}
}

this->ReduceToTridiagonalMatrixAndGetTransformation(inputMatrix, dVector, workArea1, workArea2);
this->ReduceToTridiagonalMatrixAndGetTransformation(
inputMatrix.get(), dVector.get(), workArea1.get(), workArea2.get());

const unsigned int eigenErrIndex = this->ComputeEigenValuesAndVectorsUsingQL(dVector, workArea1, workArea2);
const unsigned int eigenErrIndex =
this->ComputeEigenValuesAndVectorsUsingQL(dVector.get(), workArea1.get(), workArea2.get());

// Copy eigenvectors
k = 0;
Expand All @@ -133,11 +132,6 @@ SymmetricEigenAnalysis<TMatrix, TVector, TEigenMatrix>::ComputeEigenValuesAndVec
}
}

delete[] dVector;
delete[] workArea2;
delete[] workArea1;
delete[] inputMatrix;

return eigenErrIndex; // index of eigenvalue that could not be computed
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "itkNeighborhoodIterator.h"
#include "itkImageRegionIterator.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand Down Expand Up @@ -104,7 +105,7 @@ FastChamferDistanceImageFilter<TInputImage, TOutputImage>::GenerateDataND()
NeighborhoodIterator<TInputImage> it(r, this->GetOutput(), m_RegionToProcess);

const unsigned int center_voxel = it.Size() / 2;
auto * neighbor_type = new int[it.Size()];
const auto neighbor_type = make_unique_for_overwrite<int[]>(it.Size());
int i;
unsigned int n;
float val[ImageDimension];
Expand Down Expand Up @@ -263,7 +264,6 @@ FastChamferDistanceImageFilter<TInputImage, TOutputImage>::GenerateDataND()
}
}
}
delete[] neighbor_type;
}

template <typename TInputImage, typename TOutputImage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "itkNeighborhoodInnerProduct.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkDerivativeOperator.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand Down Expand Up @@ -147,10 +148,10 @@ GPUScalarAnisotropicDiffusionFunction<TImage>::GPUCalculateAverageGradientMagnit
kernelManager->LaunchKernel(kernelHandle, ImageDim, globalSize, localSize);

// Read back intermediate sums from GPU and compute final value
double sum = 0;
auto * intermSum = new float[bufferSize];
double sum = 0;
const auto intermSum = make_unique_for_overwrite<float[]>(bufferSize);

this->m_AnisotropicDiffusionFunctionGPUBuffer->SetCPUBufferPointer(intermSum);
this->m_AnisotropicDiffusionFunctionGPUBuffer->SetCPUBufferPointer(intermSum.get());
this->m_AnisotropicDiffusionFunctionGPUBuffer->SetCPUDirtyFlag(true); //
// CPU
// is
Expand All @@ -166,8 +167,6 @@ GPUScalarAnisotropicDiffusionFunction<TImage>::GPUCalculateAverageGradientMagnit
}

this->SetAverageGradientMagnitudeSquared(static_cast<double>(sum / static_cast<double>(numPixel)));

delete[] intermSum;
}

} // end namespace itk
Expand Down
17 changes: 6 additions & 11 deletions Modules/Filtering/ImageCompare/include/itkSTAPLEImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkSTAPLEImageFilter_hxx

#include "itkImageScanlineIterator.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand Down Expand Up @@ -58,13 +59,13 @@ STAPLEImageFilter<TInputImage, TOutputImage>::GenerateData()
// Record the number of input files.
number_of_input_files = this->GetNumberOfIndexedInputs();

auto * D_it = new IteratorType[number_of_input_files];
const auto D_it = make_unique_for_overwrite<IteratorType[]>(number_of_input_files);

auto * p = new double[number_of_input_files]; // sensitivity
auto * q = new double[number_of_input_files]; // specificity
const auto p = make_unique_for_overwrite<double[]>(number_of_input_files); // sensitivity
const auto q = make_unique_for_overwrite<double[]>(number_of_input_files); // specificity

auto * last_q = new double[number_of_input_files];
auto * last_p = new double[number_of_input_files];
const auto last_q = make_unique_for_overwrite<double[]>(number_of_input_files);
const auto last_p = make_unique_for_overwrite<double[]>(number_of_input_files);

for (i = 0; i < number_of_input_files; ++i)
{
Expand Down Expand Up @@ -250,12 +251,6 @@ STAPLEImageFilter<TInputImage, TOutputImage>::GenerateData()
m_Specificity.push_back(q[i]);
}
m_ElapsedIterations = iter;

delete[] q;
delete[] p;
delete[] last_q;
delete[] last_p;
delete[] D_it;
}
} // end namespace itk

Expand Down
32 changes: 16 additions & 16 deletions Modules/IO/MeshBase/include/itkMeshFileWriter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
#include "itkMeshConvertPixelTraits.h"
#include "itkMeshIOFactory.h"
#include "itkObjectFactoryBase.h"
#include "itkMakeUniqueForOverwrite.h"

#include "vnl/vnl_vector.h"


namespace itk
{
template <typename TInputMesh>
Expand Down Expand Up @@ -248,10 +250,10 @@ MeshFileWriter<TInputMesh>::WritePoints()

itkDebugMacro(<< "Writing points: " << m_FileName);
SizeValueType pointsBufferSize = input->GetNumberOfPoints() * TInputMesh::PointDimension;
auto * buffer = new typename TInputMesh::PointType::ValueType[pointsBufferSize];
CopyPointsToBuffer(buffer);
m_MeshIO->WritePoints(buffer);
delete[] buffer;
using ValueType = typename TInputMesh::PointType::ValueType;
const auto buffer = make_unique_for_overwrite<ValueType[]>(pointsBufferSize);
CopyPointsToBuffer(buffer.get());
m_MeshIO->WritePoints(buffer.get());
}

template <typename TInputMesh>
Expand All @@ -261,10 +263,10 @@ MeshFileWriter<TInputMesh>::WriteCells()
itkDebugMacro(<< "Writing cells: " << m_FileName);

SizeValueType cellsBufferSize = m_MeshIO->GetCellBufferSize();
auto * buffer = new typename TInputMesh::PointIdentifier[cellsBufferSize];
CopyCellsToBuffer(buffer);
m_MeshIO->WriteCells(buffer);
delete[] buffer;
using PointIdentifierType = typename TInputMesh::PointIdentifier;
const auto buffer = make_unique_for_overwrite<PointIdentifierType[]>(cellsBufferSize);
CopyCellsToBuffer(buffer.get());
m_MeshIO->WriteCells(buffer.get());
}

template <typename TInputMesh>
Expand All @@ -282,10 +284,9 @@ MeshFileWriter<TInputMesh>::WritePointData()
input->GetPointData()->Begin().Value());

using ValueType = typename itk::NumericTraits<typename TInputMesh::PixelType>::ValueType;
auto * buffer = new ValueType[numberOfComponents];
CopyPointDataToBuffer(buffer);
m_MeshIO->WritePointData(buffer);
delete[] buffer;
const auto buffer = make_unique_for_overwrite<ValueType[]>(numberOfComponents);
CopyPointDataToBuffer(buffer.get());
m_MeshIO->WritePointData(buffer.get());
}
}

Expand All @@ -304,10 +305,9 @@ MeshFileWriter<TInputMesh>::WriteCellData()
input->GetCellData()->Begin().Value());

using ValueType = typename itk::NumericTraits<typename TInputMesh::CellPixelType>::ValueType;
auto * buffer = new ValueType[numberOfComponents];
CopyCellDataToBuffer(buffer);
m_MeshIO->WriteCellData(buffer);
delete[] buffer;
const auto buffer = make_unique_for_overwrite<ValueType[]>(numberOfComponents);
CopyCellDataToBuffer(buffer.get());
m_MeshIO->WriteCellData(buffer.get());
}
}

Expand Down
10 changes: 5 additions & 5 deletions Modules/IO/MeshBase/include/itkMeshIOBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "itkVector.h"
#include "itkNumberToString.h"
#include "itkCommonEnums.h"
#include "itkMakeUniqueForOverwrite.h"

#include <string>
#include <complex>
Expand Down Expand Up @@ -686,23 +687,22 @@ class ITKIOMeshBase_EXPORT MeshIOBase : public LightProcessObject
}
else
{
auto * data = new TOutput[numberOfComponents];
const auto data = make_unique_for_overwrite<TOutput[]>(numberOfComponents);
for (SizeValueType ii = 0; ii < numberOfComponents; ++ii)
{
data[ii] = static_cast<TOutput>(buffer[ii]);
}

if (m_ByteOrder == IOByteOrderEnum::BigEndian && itk::ByteSwapper<TOutput>::SystemIsLittleEndian())
{
itk::ByteSwapper<TOutput>::SwapRangeFromSystemToBigEndian(data, numberOfComponents);
itk::ByteSwapper<TOutput>::SwapRangeFromSystemToBigEndian(data.get(), numberOfComponents);
}
else if (m_ByteOrder == IOByteOrderEnum::LittleEndian && itk::ByteSwapper<TOutput>::SystemIsBigEndian())
{
itk::ByteSwapper<TOutput>::SwapRangeFromSystemToLittleEndian(data, numberOfComponents);
itk::ByteSwapper<TOutput>::SwapRangeFromSystemToLittleEndian(data.get(), numberOfComponents);
}

outputFile.write(reinterpret_cast<char *>(data), numberOfComponents);
delete[] data;
outputFile.write(reinterpret_cast<char *>(data.get()), numberOfComponents);
}
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/IO/MeshFreeSurfer/include/itkFreeSurferAsciiMeshIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ITKIOMeshFreeSurferExport.h"

#include "itkMeshIOBase.h"
#include "itkMakeUniqueForOverwrite.h"

#include <fstream>

Expand Down Expand Up @@ -135,9 +136,9 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferAsciiMeshIO : public MeshIOBase
constexpr unsigned int numberOfCellPoints = 3;
SizeValueType index = 0;

auto * data = new T[this->m_NumberOfCells * numberOfCellPoints];
const auto data = make_unique_for_overwrite<T[]>(this->m_NumberOfCells * numberOfCellPoints);

ReadCellsBuffer(buffer, data);
ReadCellsBuffer(buffer, data.get());

for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
{
Expand All @@ -147,7 +148,6 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferAsciiMeshIO : public MeshIOBase
}
outputFile << label << '\n';
}
delete[] data;
}

/** Read cells from a data buffer, used when writting cells */
Expand Down
19 changes: 8 additions & 11 deletions Modules/IO/MeshFreeSurfer/include/itkFreeSurferBinaryMeshIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkByteSwapper.h"
#include "itkMeshIOBase.h"
#include "itkIntTypes.h"
#include "itkMakeUniqueForOverwrite.h"

#include <fstream>

Expand Down Expand Up @@ -119,7 +120,7 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferBinaryMeshIO : public MeshIOBase
void
WritePoints(T * buffer, std::ofstream & outputFile)
{
auto * data = new float[this->m_NumberOfPoints * this->m_PointDimension];
const auto data = make_unique_for_overwrite<float[]>(this->m_NumberOfPoints * this->m_PointDimension);

for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
{
Expand All @@ -130,8 +131,7 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferBinaryMeshIO : public MeshIOBase
}

itk::ByteSwapper<float>::SwapWriteRangeFromSystemToBigEndian(
data, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
delete[] data;
data.get(), this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
}

/** Write cells to utput stream */
Expand All @@ -141,13 +141,11 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferBinaryMeshIO : public MeshIOBase
{
constexpr itk::uint32_t numberOfCellPoints = 3;

auto * data = new itk::uint32_t[this->m_NumberOfCells * numberOfCellPoints];
const std::unique_ptr<itk::uint32_t[]> data(new itk::uint32_t[this->m_NumberOfCells * numberOfCellPoints]);

ReadCellsBuffer(buffer, data);
ReadCellsBuffer(buffer, data.get());
itk::ByteSwapper<itk::uint32_t>::SwapWriteRangeFromSystemToBigEndian(
data, this->m_NumberOfCells * numberOfCellPoints, &outputFile);

delete[] data;
data.get(), this->m_NumberOfCells * numberOfCellPoints, &outputFile);
}

/** Read cells from a data buffer, used when writting mesh */
Expand All @@ -174,15 +172,14 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferBinaryMeshIO : public MeshIOBase
void
WritePointData(T * buffer, std::ofstream & outputFile)
{
auto * data = new float[this->m_NumberOfPointPixels];
const auto data = make_unique_for_overwrite<float[]>(this->m_NumberOfPointPixels);

for (SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ++ii)
{
data[ii] = static_cast<float>(buffer[ii]);
}

itk::ByteSwapper<float>::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPointPixels, &outputFile);
delete[] data;
itk::ByteSwapper<float>::SwapWriteRangeFromSystemToBigEndian(data.get(), this->m_NumberOfPointPixels, &outputFile);
}

protected:
Expand Down
Loading

0 comments on commit a9bd570

Please sign in to comment.