破坏List结构的全局数组

时间:2022-01-28 07:16:41

I was trying to destruct a global array of structs.
However, it creates following message and crashes in Visual Studio 2013.
(g++ does not crash). What causes this problem?

我试图破坏全局结构数组。但是,它会在Visual Studio 2013中创建以下消息和崩溃。(g ++不会崩溃)。是什么导致这个问题?

message:

"DEBUG ASSERTION FAILED" _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

“DEBUG ASSERTION FAILED”_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

Here is the List struct:

这是List结构:

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;
    }

};

List::insert()

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++;
}

Definition of the globa array:

globa数组的定义:

List<int> a[1001];

Here is my code to destruct the global array of List structs:

这是我的代码来破坏List结构的全局数组:

loop(i,0,N+1) {
    delete &a[i];
}

1 个解决方案

#1


0  

List<int> a[1001] would not store in the heap if you're not using new.

如果你没有使用new,List a [1001]将不会存储在堆中。

Then you're trying to delete it, it raises memory corruption error, which is one of the possible reasons to get this error.

然后你试图删除它,它会引发内存损坏错误,这是导致此错误的可能原因之一。

DEBUG ASSERTION FAILED" _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

DEBUG ASSERTION FAILED“_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

Solution: Use new

解决方案:使用新的

#1


0  

List<int> a[1001] would not store in the heap if you're not using new.

如果你没有使用new,List a [1001]将不会存储在堆中。

Then you're trying to delete it, it raises memory corruption error, which is one of the possible reasons to get this error.

然后你试图删除它,它会引发内存损坏错误,这是导致此错误的可能原因之一。

DEBUG ASSERTION FAILED" _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

DEBUG ASSERTION FAILED“_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

Solution: Use new

解决方案:使用新的