程序.exe中0x010F2F1C时未处理的异常: 0xC0000005:访问冲突读取位置0xCCCCCCD0



我似乎无法弄清楚为什么我会收到这样的错误:我正在尝试实现链表并插入列表,但是在我的第三行代码中我遇到了访问错误。错误:

Unhandled exception at 0x010F2F1C in program.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.

在我的主要()

list2.InsertNode(1, test);
cout << "cout << list2 " << endl;
cout << list2 << endl;

插入方式

void NodeSLList::InsertNode(int nodeNum, const IntNode &node)
{

    IntNode *prevNode = head;
    for (int i = 1; i < nodeNum; i++)
        prevNode = prevNode->next;
    IntNode * insert;
    insert = new IntNode(node);
    prevNode->next = insert;
    insert->next = prevNode->next->next;
}

在第三行发生错误cout << list2 << endl;("<<"是重载以输出链表)

ostream & operator<<(ostream & output, NodeSLList & list)
{
    int size;
    size = list.GetSize();
    output << "List: ";
    for (int i = 1; i <= size; i++)
    {
        output << list.RetrieveNode(i).data<<"    ";
    }
    return output;
}

更具体地说,当调用<<运算符并调用 GetSize 方法时,错误发生在此处:

    p = p->next;

获取大小方法:

int NodeSLList::GetSize()
{
    // check to see if the list is empty. if 
    // so, just return 0
    if (IsEmpty()) return 0;
    int size = 1;
    IntNode *p = head;
    // compute the number of nodes and return
    while (p != tail)
    {
        // until we reach the tail, keep counting
        size++;
        p = p->next;
    }
    return size;
}

0xcc或0xcccccccc是 MSVC 用来初始化程序未显式初始化的局部变量的模式(如果设置了/GX)。

如果您的p->next在访问0xCCCCCCD0时失败,则表明您的指针 p 已0xCCCCCCCC。查看您未在此处发布的构建列表的代码,并检查是否始终将next设置为正确的值。

最新更新