销毁 List 结构的全局数组
Destructing a global array of List struct
我试图破坏一个全局结构数组。
但是,它会创建以下消息并在 Visual Studio 2013 年崩溃。
(g++ 不会崩溃)。是什么导致了这个问题?
留言:
"DEBUG ASSERTION FAILED" _BLOCK_TYPE_IS_VALID(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];
下面是我的代码,用于销毁 List 结构的全局数组:
loop(i,0,N+1) {
delete &a[i];
}
如果您不使用 List<int> a[1001]
,则 List<int> a[1001]
不会存储在堆中。
然后你试图删除它,它会引发内存损坏错误,这是出现此错误的可能原因之一。
DEBUG ASSERTION FAILED" _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
解决方法:使用new
我试图破坏一个全局结构数组。
但是,它会创建以下消息并在 Visual Studio 2013 年崩溃。
(g++ 不会崩溃)。是什么导致了这个问题?
留言:
"DEBUG ASSERTION FAILED" _BLOCK_TYPE_IS_VALID(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];
下面是我的代码,用于销毁 List 结构的全局数组:
loop(i,0,N+1) {
delete &a[i];
}
List<int> a[1001]
,则 List<int> a[1001]
不会存储在堆中。
然后你试图删除它,它会引发内存损坏错误,这是出现此错误的可能原因之一。
DEBUG ASSERTION FAILED" _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
解决方法:使用new