1818#ifndef itkVariableLengthVector_hxx
1919#define itkVariableLengthVector_hxx
2020
21+ #include " itkMakeUniqueForOverwrite.h"
2122#include " itkNumericTraitsVariableLengthVectorPixel.h"
2223#include " itkMath.h"
2324#include < cstring>
@@ -28,9 +29,9 @@ namespace itk
2829
2930template <typename TValue>
3031VariableLengthVector<TValue>::VariableLengthVector(unsigned int length)
31- : m_Data(nullptr )
32+ : m_Data(new TValue[length])
33+ , m_NumElements(length)
3234{
33- Reserve (length);
3435 // postcondition(s)
3536 itkAssertInDebugAndIgnoreInReleaseMacro (m_Data != nullptr );
3637}
@@ -58,11 +59,10 @@ VariableLengthVector<TValue>::VariableLengthVector(const ValueType * datain, uns
5859
5960template <typename TValue>
6061VariableLengthVector<TValue>::VariableLengthVector(const VariableLengthVector<TValue> & v)
61- : m_NumElements (v.Size() )
62+ : VariableLengthVector (v.m_NumElements )
6263{
6364 if (m_NumElements != 0 )
6465 {
65- m_Data = this ->AllocateElements (m_NumElements);
6666 itkAssertInDebugAndIgnoreInReleaseMacro (m_Data != nullptr );
6767 itkAssertInDebugAndIgnoreInReleaseMacro (v.m_Data != nullptr );
6868 std::copy_n (&v.m_Data [0 ], m_NumElements, &this ->m_Data [0 ]);
@@ -131,10 +131,9 @@ template <typename VariableLengthVectorExpression1, typename VariableLengthVecto
131131VariableLengthVector<TValue>::VariableLengthVector(
132132 const VariableLengthVectorExpression<VariableLengthVectorExpression1, VariableLengthVectorExpression2, TBinaryOp> &
133133 rhs)
134- : m_NumElements (rhs.Size())
134+ : VariableLengthVector (rhs.Size())
135135{
136- m_Data = this ->AllocateElements (m_NumElements);
137- // allocate Elements post-condition
136+ // VariableLengthVector(length) post-condition
138137 itkAssertInDebugAndIgnoreInReleaseMacro (m_Data != nullptr );
139138 for (ElementIdentifier i = 0 ; i < m_NumElements; ++i)
140139 {
@@ -176,23 +175,23 @@ VariableLengthVector<TValue>::Reserve(ElementIdentifier size)
176175 {
177176 if (size > m_NumElements)
178177 {
179- TValue * temp = this -> AllocateElements (size);
178+ auto temp = make_unique_for_overwrite<TValue[]> (size);
180179 itkAssertInDebugAndIgnoreInReleaseMacro (temp);
181180 itkAssertInDebugAndIgnoreInReleaseMacro (m_NumElements == 0 || (m_NumElements > 0 && m_Data != nullptr ));
182181 // only copy the portion of the data used in the old buffer
183- std::copy_n (m_Data, m_NumElements, temp);
182+ std::copy_n (m_Data, m_NumElements, temp. get () );
184183 if (m_LetArrayManageMemory)
185184 {
186185 delete[] m_Data;
187186 }
188- m_Data = temp;
187+ m_Data = temp. release () ;
189188 m_LetArrayManageMemory = true ;
190189 m_NumElements = size;
191190 }
192191 }
193192 else
194193 {
195- m_Data = this -> AllocateElements ( size) ;
194+ m_Data = new TValue[ size] ;
196195 m_NumElements = size;
197196 m_LetArrayManageMemory = true ;
198197 }
@@ -273,16 +272,16 @@ VariableLengthVector<TValue>::SetSize(unsigned int sz, TReallocatePolicy realloc
273272
274273 if (reallocatePolicy (sz, m_NumElements) || !m_LetArrayManageMemory)
275274 {
276- TValue * temp = this -> AllocateElements (sz); // may throw
275+ auto temp = make_unique_for_overwrite<TValue[]> (sz); // may throw
277276 itkAssertInDebugAndIgnoreInReleaseMacro (temp);
278277 itkAssertInDebugAndIgnoreInReleaseMacro (m_NumElements == 0 || (m_NumElements > 0 && m_Data != nullptr ));
279- keepValues (sz, m_NumElements, m_Data, temp); // possible leak if TValue copy may throw
278+ keepValues (sz, m_NumElements, m_Data, temp. get ());
280279 // commit changes
281280 if (m_LetArrayManageMemory)
282281 {
283282 delete[] m_Data;
284283 }
285- m_Data = temp;
284+ m_Data = temp. release () ;
286285 m_LetArrayManageMemory = true ;
287286 }
288287 m_NumElements = sz;
0 commit comments