fixed a memory corruption bug in CVector

This commit is contained in:
David Anderson
2007-10-26 01:03:23 +00:00
parent 36241e2905
commit 6a567f3c77
10 changed files with 420 additions and 166 deletions

View File

@ -37,12 +37,20 @@
// Vector
template <class T> class CVector
{
bool Grow()
bool Grow(size_t amount)
{
// automatic grow
size_t newSize = m_Size * 2;
if (newSize == 0)
newSize = 8; // a good init value
{
newSize = 8;
}
while (m_CurrentUsedSize + amount > newSize)
{
newSize *= 2;
}
T *newData = new T[newSize];
if (!newData)
return false;
@ -57,12 +65,16 @@ template <class T> class CVector
return true;
}
bool GrowIfNeeded()
bool GrowIfNeeded(size_t amount)
{
if (m_CurrentUsedSize >= m_Size)
return Grow();
if (m_CurrentUsedSize + amount >= m_Size)
{
return Grow(amount);
}
else
{
return true;
}
}
bool ChangeSize(size_t size)
@ -330,7 +342,7 @@ public:
bool push_back(const T & elem)
{
++m_CurrentUsedSize;
if (!GrowIfNeeded())
if (!GrowIfNeeded(1))
{
--m_CurrentUsedSize;
return false;
@ -434,13 +446,13 @@ public:
size_t ofs = where - begin();
++m_CurrentUsedSize;
if (!GrowIfNeeded())
if (!GrowIfNeeded(1))
{
--m_CurrentUsedSize;
return false;
}
++m_CurrentUsedSize;
where = begin() + ofs;
// Move subsequent entries

View File

@ -1,9 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 8.00
# Visual C++ Express 2005
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ns", "ns.vcproj", "{5B5DEFD0-28ED-4D0E-A1B0-50F9304A65DF}"
ProjectSection(ProjectDependencies) = postProject
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfiguration) = preSolution
Debug = Debug
Release = Release
EndGlobalSection
GlobalSection(ProjectDependencies) = postSolution
EndGlobalSection
GlobalSection(ProjectConfiguration) = postSolution
{5B5DEFD0-28ED-4D0E-A1B0-50F9304A65DF}.Debug.ActiveCfg = Debug|Win32
{5B5DEFD0-28ED-4D0E-A1B0-50F9304A65DF}.Debug.Build.0 = Debug|Win32
{5B5DEFD0-28ED-4D0E-A1B0-50F9304A65DF}.Release.ActiveCfg = Release|Win32
{5B5DEFD0-28ED-4D0E-A1B0-50F9304A65DF}.Release.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EndGlobalSection
GlobalSection(ExtensibilityAddIns) = postSolution
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32