注册 登录
编程论坛 C++教室

new 的使用,能运行但不知为何会弹出Dug Assertion Failed!

A306452792 发布于 2011-03-30 22:06, 669 次点击
程序代码:
#include <iostream>
using namespace std;

class Book
{
private:
    int pages;
         double price;
    char *bookname, *authors,*publishing_house;
public:
    Book(){
    bookname=new char[50];
    authors=new char[50];   
    publishing_house=new char[50];   
    }
    ~Book(){
    delete[](bookname);(bookname)=NULL;
    delete[]authors;(authors)=NULL;
    delete[]publishing_house;(publishing_house)=NULL;


    }
    void getXxx();
    void setXxx(char *b,char *a,int pa,char*p, double pr);
};
void Book::setXxx(char *b,char *a,int pa,char*p, double pr){
bookname=b;
authors=a;
pages=pa;
publishing_house=p;
price=pr;
}

void Book::getXxx(){
    cout<<"书名:"<<bookname<<" 作者:"<<authors<<" 页数:"<<pages<<""<<" 出版社: "<<publishing_house<<" 价格:"<<price<<endl;
}

int main()
{
    cout<<"定义的两本书的信息:"<<endl;
  Book mybook;
  mybook.setXxx("《C++程序设计》","谭浩强编著",485,"清华大学出版社",36.00);
    mybook.getXxx();
    cout<<endl;
mybook.setXxx("《吹牛大王历险记》","拉斯伯等编著",149,"天津人民出版社",12.80);
    mybook.getXxx();

 
    return 0;
}
9 回复
#2
loveshuang2011-03-30 23:35
用字符串的搞搞
#include <iostream>
//#include <windows.h>
#include <string>
using namespace std;

class Book
{
private:
    int pages;
    double price;
    //char *bookname, *authors,*publishing_house;
    string s1,s2,s3;
public:
    Book(){
   //  bookname=new char[50];
    // authors=new char[50];   
   //  publishing_house=new char[50];   
    }
    ~Book(){
   // delete[] bookname;//(bookname)=NULL;
   // delete[] authors;//(authors)=NULL;
   // delete[] publishing_house;//(publishing_house)=NULL;
    }
    void getXxx();
    //void setXxx(char *b,char *a,int pa,char*p, double pr);
    void setXxx(string s1,string s2,int pa,string s3, double pr);
};
void Book::setXxx(string b,string a,int pa,string p, double pr){
s1=b;
s2=a;
pages=pa;
s3=p;
price=pr;
}

void Book::getXxx(){
    cout<<"书名:"<<s1
        <<"作者:"<<s2
        <<"页数:"<<pages<<"页 "
        <<"出版社: "<<s3<<" 价格:"<<price
        <<endl;
}

int main()
{
    cout<<"定义的两本书的信息:"<<endl;
    Book mybook;
    mybook.setXxx("《C++程序设计》","谭浩强编著",485,"清华大学出版社",36.00);
    mybook.getXxx();
    cout<<endl;
    mybook.setXxx("《吹牛大王历险记》","拉斯伯等编著",149,"天津人民出版社",12.80);
    mybook.getXxx();
//    Sleep(5000);

    return 0;
}

#3
loveshuang2011-03-30 23:40
再改一次,呵呵:,在析构函数中先判断指针是否为空了再释放
#include <iostream>
using namespace std;

class Book
{
private:
    int pages;
         double price;
    char *bookname;
    char *authors;
    char *publishing_house;
public:
    Book(){
    bookname=new char[50];
    authors=new char[50];   
    publishing_house=new char[50];   
    }
    ~Book(){
        if(!bookname)
            delete[] bookname;//(bookname)=NULL;
        if(!authors)
            delete[] authors;//(authors)=NULL;
        if(!publishing_house)
            delete[] publishing_house;//(publishing_house)=NULL;
    }
    void getXxx();
    void setXxx(char *b,char *a,int pa,char*p, double pr);
};
void Book::setXxx(char *b,char *a,int pa,char*p, double pr){
bookname=b;
authors=a;
pages=pa;
publishing_house=p;
price=pr;
}

void Book::getXxx(){
    cout<<"书名:"<<bookname<<" 作者:"<<authors<<" 页数:"<<pages<<"页 "<<" 出版社: "<<publishing_house<<" 价格:"<<price<<endl;
}

int main()
{
    cout<<"定义的两本书的信息:"<<endl;
  Book mybook;
  mybook.setXxx("《C++程序设计》","谭浩强编著",485,"清华大学出版社",36.00);
    mybook.getXxx();
    cout<<endl;
mybook.setXxx("《吹牛大王历险记》","拉斯伯等编著",149,"天津人民出版社",12.80);
    mybook.getXxx();
   
//    Sleep(5000);
    return 0;
}

#4
loveshuang2011-03-30 23:52
又想到了,你传进去的就是一个字符串的指针所以定义的字符指针不用分配空间,直接用那个指针变量就可以了
#include <iostream>
using namespace std;

class Book
{
private:
    int pages;
         double price;
    char *bookname;
    char *authors;
    char *publishing_house;
public:
    Book(){
//    bookname=new char[50];
  //  authors=new char[50];   
    //publishing_house=new char[50];   
    }
    ~Book(){
//        if(!bookname)
//            delete[] bookname;//(bookname)=NULL;
//        if(!authors)
//            delete[] authors;//(authors)=NULL;
//        if(!publishing_house)
//            delete[] publishing_house;//(publishing_house)=NULL;
    }
    void getXxx();
    void setXxx(char *b,char *a,int pa,char*p, double pr);
};
void Book::setXxx(char *b,char *a,int pa,char*p, double pr){
bookname=b;
authors=a;
pages=pa;
publishing_house=p;
price=pr;
}

void Book::getXxx(){
    cout<<"书名:"<<bookname<<" 作者:"<<authors<<" 页数:"<<pages<<"页 "<<" 出版社: "<<publishing_house<<" 价格:"<<price<<endl;
}

int main()
{
    cout<<"定义的两本书的信息:"<<endl;
  Book mybook;
  mybook.setXxx("《C++程序设计》","谭浩强编著",485,"清华大学出版社",36.00);
    mybook.getXxx();
    cout<<endl;
mybook.setXxx("《吹牛大王历险记》","拉斯伯等编著",149,"天津人民出版社",12.80);
    mybook.getXxx();
   
//    Sleep(5000);
    return 0;
}

#5
yuccn2011-03-31 00:04
把 char *bookname, *authors,*publishing_house;三个成员改成
char bookname[50];
char authors[50];
char publishing_house[50] 就可以了,没有必要用new在堆中申请内存 (最好还是不要用50,用64,也就是2的N次方的值)

不管怎么 这个函数都是不合法的
void Book::setXxx(char *b,char *a,int pa,char*p, double pr){
bookname=b; // strcpy(bookname,b);
authors=a;
pages=pa;
publishing_house=p;
price=pr;
}

应该改成用strcopy拷贝过去。

#6
A3064527922011-03-31 11:27
我想到了,好像直接把那个析构函数放在void setXxx()函数之后就行了,因为构造函数刚分配地址,void setXxx()还没使用就用析构函数删除了内存,导致的错误吧.
#7
hellovfp2011-03-31 12:04
尽量用string而不要用C-style字符串。
#8
yuccn2011-03-31 13:03
回 6 楼。你理解还是有错误
void setXxx() 把栈分配的内存交给了bookname 等成员,在对象析构的时候,调用了delete bookname;也就是delete了一个栈的内存空间。也就是错误的根本原因

如果你对内存管理足够理解,你怎么用都没有关系的

建议你还是先学习下堆栈内存的分配差别吧
#9
A3064527922011-03-31 19:55
的确,因为刚学C++,很多不会
#10
lucky5635912011-04-02 07:55
最好先调试一下
1