简体   繁体   English

表达式_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)错误

[英]Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error

After adding parameter by reference "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" debug assertion failed. 通过引用“ _BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)”添加参数后,调试声明失败。

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. 而不是std :: int32_t中位数(Stack p_stack)之后,出现“ _BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)”行。

If your median function takes Stack by value the compiler has to make a copy of the original Stack . 如果您的median函数按值使用Stack ,则编译器必须复制原始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. 因为您没有在Stack定义副本构造函数,所以编译器生成了自己的副本,它会逐个成员复制,这意味着它只是将指针的值复制过来,这样Stack两个实例现在指向同一位内存。 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. 然后,当中median函数完成执行时,将调用副本的析构函数并删除内存,因此,现在您主体中的原始Stack指向已释放的块,从而导致错误。

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. 当中median获取引用时,不会进行复制-它获取对main原始Stack的引用,并且不会调用析构函数。

I suggest you change your code so that median and the other function take a const reference to Stack as their parameters. 我建议您更改代码,以便中median和其他函数将对Stackconst引用作为其参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM