代码之家  ›  专栏  ›  技术社区  ›  Peter Hwang

销毁列表结构的全局数组

  •  0
  • Peter Hwang  · 技术社区  · 7 年前

    我试图破坏一个全局结构数组。
    然而,它在Visual Studio 2013中创建了以下消息并崩溃。

    消息:

    “调试断言失败”\u BLOCK\u TYPE\u有效(pHead->nBlockUse)

    template <typename T>
    struct List {
        struct ListNode {
            ListNode() : item(0), next(0) {}
            ListNode(T x) : item(x), next(0) {}
            T item;
            ListNode* next;
        };
        T operator[](int idx);
        void insert(T x);
        T get(int idx);
        void print();
        List() : head(0),tail(0),size(0) {}
        ListNode* head;
        ListNode* tail;
        int size;
    
        ~List() {
            ListNode* h = head;
            while(h) {
                ListNode* n = h->next;
                delete h;
                h = n;
            }
            head = NULL;
            tail = NULL;
            size = 0;
        }
    
    };
    

    列表::插入()

    template <typename T>
    void List<T>::insert(T x) {
        if(head == NULL) {
            head = new ListNode(x);
            tail = head;
        } else {
            tail->next = new ListNode(x);
            tail = tail->next;
        }
        size++;
    }
    

    List<int> a[1001];
    

    下面是我用来分解列表结构的全局数组的代码:

    loop(i,0,N+1) {
        delete &a[i];
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Shiu Chun Ming    7 年前

    List<int> a[1001] 如果不使用,则不会存储在堆中 new .

    调试断言失败“\u BLOCK\u TYPE\u IS\u VALID(pHead->nBlockUse)

    解决方案:使用