c++ - 为什么这段代码在 Linux 中运行良好但在 Windows 中却失败了?

标签 c++ linux windows crash linked-list

我写了一些链表代码: 链表定义:

struct Node {
    // data is used to store an integer value in this node
    int data;
    // a pointer points to the next node
    Node* link;
    // inializes the node with a 0 value in data and a null pointer in link
    Node() : data(0), link(NULL) {};
    // destructor release space allocated to the linked list
    ~Node() {delete link;}
};

显示链表:

void display_and_count(Node* aLink) {
    cout << "Entries: ";
    int nodeNumber = 0;                         // elements number of linked list
    for(Node* iterator = aLink; iterator->link != NULL; iterator=iterator->link) {
        cout << iterator->data << ", ";
        nodeNumber++;
    }
    cout << "contans " << nodeNumber << " nodes." << endl;
}// end display_and_count

现在我编写了一个函数,根据阈值将一个链表拆分为两个 LESS 和 MORE,并删除原始链表中的节点:

void split_them_up(Node* aLink, Node* less, Node* more, int threshold) {
    Node* lessHead = less;                              // head of less
    Node* moreHead = more;                              // head of more
    bool isThresholdInALink = false;                    // store if threshold is an element of aLink
    for(Node* iterator = aLink; iterator->link != NULL; iterator = iterator->link) {
        if(iterator->data < threshold) {
            less->data = iterator->data;
            less->link = new Node;
            less = less->link;
        }
        else if(iterator->data > threshold) {
            more->data = iterator->data;
            more->link = new Node;
            more = more->link;
        }
        else {
            isThresholdInALink = true;
        }
    } // end for(Node* iterator = aLink; iterator->link != NULL; iterator = iterator->link)

    less = lessHead;
    more = moreHead;

    delete aLink;
    // If threshold is an element of aLink, then the new linked list contains the only threshold.
    // If threshold isn't in aLink, then the new linked list contains nothing
    aLink = new Node;
    if(isThresholdInALink) {
        aLink->data = threshold;
        aLink->link = new Node;
    } // end if(isThresholdInALink)*/
} // end split_them_up

然后这是主要功能:

int main() {
    Node* ENTRIES = new Node;           // define a linked list

    get_input(ENTRIES);
    display_and_count(ENTRIES);

    Node* less = new Node;              // define less list
    Node* more = new Node;              // define more list
    cout << "Enter a threshold: ";
    int thd;                            // threshold
    cin >> thd;
    split_them_up(ENTRIES, less, more, thd);

    cout << "Less list: " << endl;
    display_and_count(less);
    cout << "More list: " << endl;
    display_and_count(more);
    cout << "ENTRIES: " << endl;
    display_and_count(ENTRIES);
}

get_input 函数从用户和-1 到结束获取一些整数:

void get_input(Node* aLink) {
    Node* head = aLink;                 // head of linked list
    int capacity=1;                     // the capacity of intArray
    int* intArray = new int[capacity];  // an array stores user input
    int size=0;                         // actual number of elements stored in the intArray

    cout << "Input some integers, -1 to end: ";
    while(true) {
        int input;
        cin >> input;
        if(input == -1) break;
        if(!isContained(intArray, size, input)) {
            intArray[size]=input;
            size++;
            // if size meets capacity, double capacity
            if(size >= capacity) {
                int* temp = new int[capacity];
                int oldCapacity = capacity;
                for(int i=0; i < oldCapacity; i++) temp[i]=intArray[i];
                delete[] intArray;
                capacity = 2*capacity;
                intArray = new int[capacity];
                for(int i=0; i < oldCapacity; i++) intArray[i]=temp[i];
                delete[] temp;
            } // end if(size >= capacity)
        } // end if(!contained(intArray, size, input))
    } // end while(true)

    for(int i=0; i<size; i++) {
        aLink->data = intArray[i];
        aLink->link = new Node;
        aLink = aLink->link;
    }
    delete[] intArray;
    aLink = head;
} // end get_input

包含:

bool isContained(int* array, int aSize, int n) {
    for(int i=0; i<aSize; i++) {
        if(array[i] == n) return true;
    }
    return false;
} // end isContained

在Linux系统中执行时一切正常。但在 Windows 中,它会在 split_them_up 之后在 ENTRIES 中显示一个随机值,程序将崩溃并给出“访问冲突读取位置”。

最佳答案

我不知道这是否能解决问题,但我很确定您不希望节点使用这样的析构函数。

  1. 没有新的,所以没有必要删除。
  2. 如果删除列表中的某个节点会触发删除下一个节点、下一个节点、下一个节点……,您将如何实现?

关于c++ - 为什么这段代码在 Linux 中运行良好但在 Windows 中却失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691847/

相关文章:

c++ - cpprestsdk 使用 vcpkg、cmake - 找不到包含文件

c++ - 点 Sprite Alpha 混合问题

java - java.lang.ClassNotFoundException 或无法找到或加载主类错误

windows - 如何将 Neo4j 2.0+ 安装为 Windows 服务

c++ - 目标文件在 Linux 上比在 macOS 或 Windows 上大 2.5 倍

c++ - 如何锁定使用带有初始化列表的构造函数的类的成员函数

c++ - BitBlt 仅捕获部分屏幕

linux - 使用公共(public) SSH key 连接到本地主机失败

linux - 复制文件的shell脚本

linux - PyQt5错误 "PyCapsule_GetPointer called with incorrect name"