标签:c destructor constructor aggregate linked-list
我正在学习使用链表实现Stack.这是节点类:
class StudentInfo {
public:
string id, name, course;
double GPA;
StudentInfo *next;
};
这是Stack类:
class StackLinkedList {
public:
StudentInfo* top; //pointer to point to the top node
int size; //variable to keep the size of the stack
//constructor
StackLinkedList() {
this->size = 0;
this->top = NULL;
}
//destructor
~StackLinkedList() {
StudentInfo *current = top;
while (top) {
current = current->next;
delete top;
top = current;
}
}
//to add item into stack - push on top
void push(StudentInfo *newStudent) {
if (!top) {
top = newStudent;
return;
}
newStudent->next = top;
top = newStudent;
size++;
}
void main() {
StudentInfo s1("phi", "123", "computer science", 4.0);
StudentInfo s2("abc", "123", "software engineer", 4.0);
StudentInfo s3("zxc", "123", "business management", 4.0);
StackLinkedList list;
StudentInfo *ptr;
ptr = &s1;
list.push(ptr);
ptr = &s2;
list.push(ptr);
ptr = &s3;
list.push(ptr);
};
当我尝试在push()和printAll()上运行单元测试时,一切都很好.但是,在调用了析构函数()之后,出现错误Debug Assertion Failed … is_block_type_valid(header-> _block_use).并且调试器在delete top处触发了断点;
//destructor
~StackLinkedList() {
StudentInfo *current = top;
while (top) {
current = current->next;
delete top; //here
top = current;
}
}
如果我把top = NULL;在删除top;之前,错误消失了.所以,我对top = NULL有点困惑;声明.
编辑:NodeType的构造函数
StudentInfo(string id, string name, string course, double gpa) {
this->id = id; this->name = name; this->course = course; this->GPA = gpa; this->next = NULL;
}
解决方法:
您通过尝试删除自动存储持续时间的对象来调用未定义的行为.
int main() {
StudentInfo s1("phi", "123", "computer science", 4.0);
StudentInfo s2("abc", "123", "software engineer", 4.0);
StudentInfo s3("zxc", "123", "business management", 4.0);
StackLinkedList list;
StudentInfo *ptr;
ptr = &s1;
list.push(ptr);
ptr = &s2;
list.push(ptr);
ptr = &s3;
list.push(ptr);
};
如您所见,s1,s2,s3是自动存储持续时间的对象(也就是说,编译器会在其生命周期结束时自动调用它们的析构函数).
然而,你将它们的地址传递给list,它的析构函数会在破坏时删除其链表详细信息中的所有指针….永远不要在指向未使用new创建的对象的指针上调用delete.
一些额外的说明:
> void main()在C中是非法的.你使用的是较旧的编译器吗? ..
>每个对象都应该管理其资源.例如,std :: forward_list使用allocator在内部管理其节点的分配.我建议您重新设计StackLinkedList以在内部管理其节点,以便客户端永远不会打扰生命周期.
>你应该阅读Rule of Three和The Rule of Five
>您的代码中还有其他一些错误,我没有碰过.
标签:c,destructor,constructor,aggregate,linked-list 来源: https://codeday.me/bug/20190828/1750039.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。