After adding parameter by reference "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" debug assertion failed.
class Stack
{
private:
const std::uint32_t m_maxElement;
protected:
std::uint32_t m_currentElement;
int *m_tab;
public:
Stack(std::uint32_t p_maxElement);
virtual ~Stack();
void push(int p_element);
std::int32_t pop(void);
std::uint32_t size(void) const;
};
Stack::Stack(std::uint32_t p_maxElement) : m_maxElement(p_maxElement), m_tab(new int[m_maxElement]), m_currentElement(0u)
{}
Stack::~Stack()
{
if (m_tab) delete[] m_tab;
}
void Stack::push(int p_element)
{
if (m_currentElement < m_maxElement)
{
m_tab[m_currentElement] = p_element;
++m_currentElement;
}
}
std::int32_t Stack::pop()
{
if (m_currentElement > 0u)
{
return m_tab[--m_currentElement];
}
}
std::uint32_t Stack::size() const
{
return m_currentElement;
}
/*************************************************************************/
std::int32_t median(Stack p_stack)
{
std::vector<std::int32_t> l_container(p_stack.size());
while (p_stack.size())
{
l_container.push_back(p_stack.pop());
}
std::sort(l_container.begin(), l_container.end());
int l_containerSize = l_container.size();
int l_middleIndex = l_containerSize / 2;
if (l_containerSize % 2 == 0)
{
int l_firstMiddleElement = l_container[l_middleIndex - 1];
int l_secondMiddleElement = l_container[l_middleIndex];
return (l_firstMiddleElement + l_secondMiddleElement) / 2;
}
else
{
return l_container[l_middleIndex];
}
return 0;
}
std::int32_t arithmeticAverage(Stack p_stack)
{
std::int32_t l_sum = 0;
int l_stackSize = p_stack.size();
while (p_stack.size())
{
l_sum = p_stack.pop();
}
return l_sum / l_stackSize;
}
int main()
{
Stack firstStack(10);
firstStack.push(2);
firstStack.push(4);
firstStack.push(8);
std::cout << "firstStack.pop() = " << firstStack.pop() << std::endl;
std::cout << "median(firstStack) = " << median(firstStack) << std::endl
std::cout << "arithmeticAverage(firstStack) = " << arithmeticAverage(firstStack) << std::endl;
return 0;
}
In case of
std::int32_t median(Stack &p_stack)
is ok. Instead of std::int32_t median(Stack p_stack) After that line "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" appears.
If your median
function takes Stack
by value the compiler has to make a copy of the original Stack
. Because you have not defined a copy constructor in Stack
the compiler generated its own which does member wise copy which means it just copies the value of the pointer over so that two instances of Stack
now point at the same bit of memory. Then when the median
function finishes execution the destructor of the copy is called and deletes the memory therefore now the original Stack
in your main is pointing at a block that has been freed which causes the error.
When median
takes a reference there is no copy being made - it gets a reference to the original Stack
in main
and no destructor gets called.
I suggest you change your code so that median
and the other function take a const
reference to Stack
as their parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.