C++堆损坏,但仅在单元测试项目中

C++ Heap Corruption but only in Unit Test Project

本文关键字:单元测试 项目 损坏 C++      更新时间:2023-10-16

我正在使用模板实现一个自定义堆栈。但是我遇到了一个问题,让我质疑内存安全性。析构函数在正常使用时工作正常,但在单元测试时会触发堆损坏。特别是在预期引发异常的测试中。

所有堆栈代码。

#pragma once
#include "StackOverflowException.h"
#include "StackEmptyException.h"
template <class T>
class Stack
{
public:
const static int MAX_SIZE = 1000;
Stack() : m_array(new T[MAX_SIZE]), m_size(0), m_top(0) {};
Stack(const Stack<T>& _other)
{
m_array = new T[MAX_SIZE];
for(auto i = 0; i < _other.size(); ++i)
{
m_array[i] = _other.m_array[i];
}
m_size = _other.size();
m_top = _other.m_top;
}
~Stack()
{
delete[] m_array;
}
void push(T _item)
{
if (m_size + 1 == MAX_SIZE + 1)
{
throw StackOverflowException();
}
m_array[++m_top] = _item;
++m_size;
}
T pop()
{
if(m_top != 0)
{
--m_size;
T item =  m_array[m_top];
--m_top;
return item;
}
throw StackEmptyException();
}
T peek() const
{
if (m_size == 0) throw StackEmptyException();
T item = m_array[m_top];
return item;
}
bool empty() const
{
return m_size == 0;
}
size_t size() const
{
return m_size;
}
private:
T* m_array;
size_t m_size;
int m_top;
};

导致堆损坏的单元测试。


TEST_METHOD(CustomStackPopException)
{
Stack<int> stack;
Assert::ExpectException<StackEmptyException>([&] { stack.pop(); });
}
TEST_METHOD(CustomStackPeekException)
{
Stack<int> stack;
Assert::ExpectException<StackEmptyException>([&] { int a = stack.peek(); });
}
TEST_METHOD(CustomStackOverflowException)
{
Stack<int> stack;
const auto functor = [&]
{
for(auto i = 0; i <= Stack<int>::MAX_SIZE; ++i)
{
stack.push(i);
}
};
Assert::ExpectException<StackOverflowException>(functor);
}

当它到达这些测试中的第一个时,它将抛出一个弹出警告,说:

HEAP CORRUPTION DETECTED: after Normal block(#279) at 0x0000020298CBC60.
CRT detected that the application wrote to memory after end of heap buffer.

我尝试在堆上创建堆栈对象,但这会导致相同的错误。

此成员函数

void push(T _item)
{
if (m_size + 1 == MAX_SIZE + 1)
{
throw StackOverflowException();
}
m_array[++m_top] = _item;
++m_size;
}

已经不正确了。假设 MAX_SIZE 等于 1,m_size 等于 0。在这种情况下,m_size + 1 不等于 MAX_SIZE + 1,并且有

m_array[1] = _item;

在分配的阵列之外。

编辑:按以下方式更改方法后

void push(T _item)
{
if (m_size + 1 > MAX_SIZE)
{
throw StackOverflowException();
}
m_array[++m_top] = _item;
++m_size;
}

在我指出它的问题之后,实际上该方法也有同样的问题。事实上,你什么也没做。

此外,您的堆栈不会用索引 0 填充项目。所以在构造函数中

Stack(const Stack<T>& _other)
{
m_array = new T[MAX_SIZE];
for(auto i = 0; i < _other.size(); ++i)
{
m_array[i] = _other.m_array[i];
}
m_size = _other.size();
m_top = _other.m_top;
}

在位置 0 处访问具有不确定值的项目

成员函数推送应该像这样定义

void push( const T &_item)
{
if (m_size == MAX_SIZE)
{
throw StackOverflowException();
}
m_array[m_top++] = _item;
++m_size;
}

其他成员功能应根据成员功能推送进行更改。

多亏了用户的组合,问题已经解决。是推送功能导致了错误。

推送功能已从:

void push(T _item)
{
if (m_size + 1 == MAX_SIZE + 1)
{
throw StackOverflowException();
}
m_array[++m_top] = _item;
++m_size;
}

自:

void push(T _item)
{
if (m_size == MAX_SIZE)
{
throw StackOverflowException();
}
m_array[m_size] = _item;
++m_size;
++m_top;
}

构造函数已从:

Stack() : m_array(new T[MAX_SIZE]), m_size(0), m_top(0) {};

Stack() : m_array(new T[MAX_SIZE]), m_size(0), m_top(-1) {};

现在大小和顶部不再相同,m_top实际上指向堆栈的顶部。

使用分配编号

HEAP CORRUPTION DETECTED: after Normal block(#279) at 0x0000020298CBC60.
CRT detected that the application wrote to memory after end of heap buffer.

然后在启动插入的某个地方

_CrtSetBreakAlloc(279);

这将向您显示内存的分配位置,并应提供有关损坏位置的重要提示。

为什么要自己手卷?

不要自己实施stack

STL 已经有该容器

template <class T>
using my_stack = std::stack< T, std::vector<T> >;