Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并两个链表会在MSVC中抛出_CrtlsValidHeapPointer(块) 我在自学C++中的数据结构,我现在的挑战是创建和合并两个链表。但是,Microsoft Visual Studio引发以下错误: Debug Assertion Failed! Program: LinkedList\Debug\LinkedList.exe File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 904 Expression: _CrtlsValidHeapPointer(block)_C++_Visual Studio_Linked List - Fatal编程技术网

合并两个链表会在MSVC中抛出_CrtlsValidHeapPointer(块) 我在自学C++中的数据结构,我现在的挑战是创建和合并两个链表。但是,Microsoft Visual Studio引发以下错误: Debug Assertion Failed! Program: LinkedList\Debug\LinkedList.exe File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 904 Expression: _CrtlsValidHeapPointer(block)

合并两个链表会在MSVC中抛出_CrtlsValidHeapPointer(块) 我在自学C++中的数据结构,我现在的挑战是创建和合并两个链表。但是,Microsoft Visual Studio引发以下错误: Debug Assertion Failed! Program: LinkedList\Debug\LinkedList.exe File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 904 Expression: _CrtlsValidHeapPointer(block),c++,visual-studio,linked-list,C++,Visual Studio,Linked List,然而,它在Wandbox中似乎运行良好:哪里出了问题 这是源代码: #include <iostream> #include <cstdlib> class Node { public: int data; Node* next; }; class LinkedList { public: LinkedList() { head = nullptr; } ~LinkedList() { Node

然而,它在Wandbox中似乎运行良好:哪里出了问题

这是源代码:

#include <iostream>
#include <cstdlib>

class Node {
public:
    int data;
    Node* next;
};

class LinkedList {
public:
    LinkedList() {
        head = nullptr;
    }

    ~LinkedList() {
        Node* temp = head;

        while (head) {
            head = head->next;
            delete temp;
            temp = head;
        }
    };

    void addNode(int value);
    void display();
    void merge(LinkedList& list2);

private:
    Node* head;
};

void LinkedList::addNode(int value) {
    Node* newnode = new Node();
    newnode->data = value;
    newnode->next = nullptr;

    if (head == nullptr) {
        head = newnode;
    } else {
        Node* temp = head; // head is not NULL

        while (temp->next != nullptr) {
            temp = temp->next; // go to end of list
        }

        temp->next = newnode; // linking to newnode
    }
}

void LinkedList::display() {
    if (head == nullptr) {
        std::cout << "List is empty!" << std::endl;
    } else {
        Node* temp = head;
        while (temp != nullptr) {
            std::cout << temp->data << " ";
            temp = temp->next;
        }

        std::cout << std::endl;
    }
}

void LinkedList::merge(LinkedList& list2) {
    Node* node = new Node();
    node->next = nullptr;
    Node* temp = node;

    Node* head1 = head;
    Node* head2 = list2.head;

    while (head1 != nullptr && head2 != nullptr) {
        if (head1->data <= head2->data) {
            temp->next = head1;
            temp = temp->next;
            head1 = head1->next;
        } else {
            temp->next = head2;
            temp = temp->next;
            head2 = head2->next;
        }
    }

    while (head1 != nullptr && head2 == nullptr) {
        temp->next = head1;
        temp = temp->next;
        head1 = head1->next;
    }

    while (head2 != nullptr && head1 == nullptr) {
        temp->next = head2;
        temp = temp->next;
        head2 = head2->next;
    }

    temp = temp->next;
    delete temp;

    head = node->next;
}

int main() {
    LinkedList list;

    list.addNode(1);
    list.addNode(2);

    std::cout << "Linked List Data: " << list.display() << std::endl;

    LinkedList list2;
    list2.addNode(3);

    list.merge(list2);

    list.display();

    return 0;
}
#包括
#包括
类节点{
公众:
int数据;
节点*下一步;
};
类链接列表{
公众:
LinkedList(){
水头=零PTR;
}
~LinkedList(){
节点*温度=头部;
while(head){
头部=头部->下一步;
删除临时文件;
温度=水头;
}
};
void addNode(int值);
void display();
作废合并(LinkedList和list2);
私人:
节点*头;
};
void LinkedList::addNode(int值){
Node*newnode=newnode();
新建节点->数据=值;
newnode->next=nullptr;
if(head==nullptr){
头=新节点;
}否则{
Node*temp=head;//head不为NULL
while(临时->下一步!=nullptr){
temp=temp->next;//转到列表末尾
}
temp->next=newnode;//链接到newnode
}
}
void LinkedList::display(){
if(head==nullptr){
std::cout next=head1;
温度=温度->下一步;
head1=head1->next;
}否则{
温度->下一步=头2;
温度=温度->下一步;
head2=head2->next;
}
}
while(head1!=nullptr&&head2==nullptr){
temp->next=head1;
温度=温度->下一步;
head1=head1->next;
}
while(head2!=nullptr&&head1==nullptr){
温度->下一步=头2;
温度=温度->下一步;
head2=head2->next;
}
温度=温度->下一步;
删除临时文件;
head=节点->下一步;
}
int main(){
链接列表;
addNode(1);
addNode(2);

std::coutMSVC错误表示内存有问题。在这种情况下,似乎要删除内存两次。释放指针两次是未定义的行为,不需要编译器/运行时来诊断问题

您的合并似乎需要排序列表,但
addNode
不会强制执行此操作。此外,一旦合并完成,
list2
的节点将同时包含在返回的列表和原始列表2中。当
LinkedList
析构函数运行时,您将(尝试)删除这些节点两次。调试版本中的Microsoft编译器可以告诉您这些问题


内存泄漏也存在问题(例如,
merge
中分配的节点未被释放)。

表达式:\u CrtIsValidHeapPointer(block)表示内存已损坏,可能是由于写入超过缓冲区的结尾或写入超过类的结尾,因为写入程序假定类大于分配的大小(例如,当它需要一个派生类并获取一个基类或另一个类的对象时)

请打开调试->窗口->异常设置,在异常设置窗口中,展开一类异常的节点->公共语言运行时异常(即.NET异常),然后选中该类别System.AccessViolationException中特定异常的复选框。如果不知道要选择哪个异常,还可以选择整个异常类别。然后,您可以看到初始问题

有关如何管理异常的更多信息,请参阅以下文档: