我一用 delete 就运行出错,何解?

chl 2001-11-29 05:16:23
小弟做C++还是新手,做了个简单的链表,虽然插入、删除、等功能都实现了,但发现只要我的代码中一出现,如下注释了的 delete .. 就运行时报错了,请指点。

#include<iostream.h>

class Node
{
char* mesg;
Node* nextNode;
public :
Node(char* msg);
void setNode(Node* n);
Node* getNode();
char* getMesg();
~Node();
};

char* Node::getMesg(){
return mesg;
}

void Node::setNode(Node* n){
nextNode=n;
}

Node* Node::getNode(){
return nextNode;
}

Node::Node(char* msg)
{
mesg=msg;
nextNode=NULL;
}

Node::~Node(){
delete mesg;
delete nextNode;
cout<<"\n**node delet**\n";
}

// List started.

class List
{
Node* head,* tail;
unsigned int nCount;
void remove(Node* n);
public :
List();
int nodeCount();
void insert(unsigned int count,Node* n);
void add(Node* n);
void removeAll();
void delet(unsigned int count);
void show();
~List();
};

List::List(){
head=new Node("It's head here.");
tail=head;
head->setNode(NULL);
nCount=0;
}

List::~List(){
delete head;
delete tail;
cout<<"\n**delete List**\n";
}

void List::add(Node* n){
if(nCount==0){
head->setNode(n);
tail=n;
}
else{
tail->setNode(n);
tail=n;
}
tail->setNode(NULL);
nCount++;
}

void List::insert(unsigned int count,Node* n){
if(count==0){
cout<<"只从1开始。"<<endl;
return ;
}
if(count>nCount+1){
cout<<"没有那么多个节点。"<<endl;
return ;
}
if(count!=nCount+1){
Node* node=head;
for(unsigned int i=1;i<count;i++){
node=node->getNode();
}
n->setNode(node->getNode());
node->setNode(n);
List::nCount++;
}
else{List::add(n);}
}

void List::delet(unsigned int count){
if(count==0){
cout<<"从1开始。"<<endl;
return ;
}
if(count>nCount){
cout<<"没有那么多个节点。"<<endl;
return ;
}
if(count!=nCount){
Node* node=head;
for(unsigned int i=1;i<count;i++){
node=node->getNode();
}
Node* nn=node->getNode();
node->setNode(nn->getNode());
//delete nn;
nCount--;
}
else{
Node* node=head;
for(unsigned int i=1;i<nCount;i++){
node=node->getNode();
}
Node* nn=tail;
tail=node;
tail->setNode(NULL);
//delete nn;
nCount--;
}
}

void List::removeAll(){
tail=head;
List::remove(head->getNode());
nCount=0;
tail->setNode(NULL);
}

void List::remove(Node* n){
if(n->getNode()!=NULL) {
remove(n->getNode());
}
//delete n;
}

void List::show(){
if(head->getNode()==NULL){
cout<<"Nothing in it.\n";
return;
}
Node* n=head->getNode();
cout<<"\n";
while(n!=NULL){
cout<<" "<<n->getMesg();
n=n->getNode();
}
cout<<"\n\n";
}

//class finished, main method then.

main(){
List* l;
l=new List();
l->show();
Node * n1=new Node("1");
l->add(n1);
l->add(new Node("2"));
l->insert(3,new Node("3"));
l->show();
l->delet(3);
l->show();
l->removeAll();
l->show();
//delete n1;
//delete l;
return 1;
}
...全文
120 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chl 2001-12-01
  • 打赏
  • 举报
回复
谢谢各位的帮助。以后还请多多指教。
jellimy 2001-11-30
  • 打赏
  • 举报
回复
同意!
不是自己new的就不需要自己delete;delete之前判断参数是否为空。
对malloc和free同样适用。
leizhengdeng 2001-11-30
  • 打赏
  • 举报
回复
同意楼上的
#include <iostream.h>
#include <string.h>

class Node
{
private:
char* mesg;
Node* nextNode;
public :
Node(char* msg);
~Node();
void setNode(Node* n);
Node* getNode();
char* getMesg();

};

char* Node::getMesg()
{
return mesg;
}

void Node::setNode(Node* node)
{
nextNode = node;
}

Node* Node::getNode()
{
return nextNode;
}

Node::Node(char* msg)
{
mesg = new char[strlen(msg)+1];
strcpy(mesg, msg);
nextNode=NULL;
}

Node::~Node()
{
if (mesg != NULL)
{
delete mesg;
}
if (nextNode != NULL)
{
delete nextNode;
}
cout<<"\n**node deleted**\n";
}

int main()
{
Node* node=new Node("hello");
cout<<"message:"<<node->getMesg()<<"\n";
delete node;
return 1;
}
hz129 2001-11-29
  • 打赏
  • 举报
回复
改成:
......
Node::Node(char* msg)
{
mesg = new char[strlen(msg)+1];
strcpy(mesg, msg);
nextNode=NULL;
}
......

你创建node的时候传进去的"hello"是一个const指针,它不是你自己用new创建的,但你的node删除的时候执行delete mesg;试图删除它,所以会出错。
new 和 delete必须成对出现。
chl 2001-11-29
  • 打赏
  • 举报
回复
那我把它弄短些,希望大虾能够帮帮我。
如果在 main() 里面的"delete node"没被注释,也一样运行时出错,改成"delete[] node"也一样不行。究竟是什么回事?

#include<iostream.h>

class Node
{
char* mesg;
Node* nextNode;
public :
Node(char* msg);
void setNode(Node* n);
Node* getNode();
char* getMesg();
~Node();
};

char* Node::getMesg(){
return mesg;
}

void Node::setNode(Node* n){
nextNode=n;
}

Node* Node::getNode(){
return nextNode;
}

Node::Node(char* msg)
{
mesg=msg;
nextNode=NULL;
}

Node::~Node(){
delete mesg;
delete nextNode;
cout<<"\n**node delet**\n";
}

main(){
Node* node=new Node("hello");
cout<<"message:"<<node->getMesg()<<"\n";
//delete node;
return 1;
}
leizhengdeng 2001-11-29
  • 打赏
  • 举报
回复
For common variable and class, you should use delete;
for array of variable and class, you should use delete [].
zx_sanjin 2001-11-29
  • 打赏
  • 举报
回复
太长了吧~~~~~
对基本变量的指针, delete 与 delete[]可通用;
对类或或结构的指针, 就必须用delete[]~~~~~
chl 2001-11-29
  • 打赏
  • 举报
回复
help!

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧