fix realloc when initial size was 0 (bug reported by Jens Mueller)

This commit is contained in:
Gael Guennebaud 2009-05-07 13:13:42 +00:00
parent 4f0af00e51
commit 6dffdca123

View File

@ -243,6 +243,13 @@ class SparseMatrix
float reallocRatio = 1; float reallocRatio = 1;
if (m_data.allocatedSize()<=m_data.size()) if (m_data.allocatedSize()<=m_data.size())
{
// if there is no preallocated memory, let's reserve a minimum of 32 elements
if (m_data.size()==0)
{
m_data.reserve(32);
}
else
{ {
// we need to reallocate the data, to reduce multiple reallocations // we need to reallocate the data, to reduce multiple reallocations
// we use a smart resize algorithm based on the current filling ratio // we use a smart resize algorithm based on the current filling ratio
@ -254,6 +261,7 @@ class SparseMatrix
// 2) avoid to allocate too much memory when the matrix is almost empty // 2) avoid to allocate too much memory when the matrix is almost empty
reallocRatio = std::min(std::max(reallocRatio,1.5f),8.f); reallocRatio = std::min(std::max(reallocRatio,1.5f),8.f);
} }
}
m_data.resize(m_data.size()+1,reallocRatio); m_data.resize(m_data.size()+1,reallocRatio);
if (!isLastVec) if (!isLastVec)