【C++】【VScode】控制台闪退,静态空间分配造成内存不足

使用链表读取文件(.csv 大小1663kb,一万多条数据),控制台闪退,空间不足。
原因:char BookName[1000];等,绝大部分字符串都不会超过1000字节,直接静态字符串浪费空间。

class Book						//存储图书信息的类
{
public:
	char BookName[1000];			    //书名,用于查找	
	char Writer[1000];			    //作者名,用于查找
	char PublishDate[20];		//出版日期,用于查找
	char ISBN[30];				//ISBN 号,用于查找
	char ebook[2];				//电子书 
	char paperbook[2];			//纸质书  
	char Publisher[1000];		    //出版商,用于查找
	char BriefIntroduction[1000];    //该书内容简介
	int i_o;					//判断书是否借出,0 为在架,1 为借出
	Book *next;					//指向下一个节点的指针
};

改为,字符指针,动态分配空间

class Book						//存储图书信息的类
{
public:
	char *BookName;			    //书名,用于查找	
	char *Writer;			    //作者名,用于查找
	char PublishDate[20];		//出版日期,用于查找
	char ISBN[30];				//ISBN 号,用于查找
	char ebook[2];				//电子书 
	char paperbook[2];			//纸质书  
	char *Publisher;		    //出版商,用于查找
	char *BriefIntroduction;    //该书内容简介
	int i_o;					//判断书是否借出,0 为在架,1 为借出
	Book *next;					//指向下一个节点的指针
};

static void save_file()					//存储文件
	{
		Book *p;           //临时节点
	    fstream file;
		string s1,s2,s3,s4,s5,s6,s7,s8,s9;
		char a1[1000];
		file.open("library//big.csv",ios::in | ios::out);
		if(!file)
		{
			cout<<"[save_file] big.csv not open file"<<endl;
			return;
		}
		getline(file,s1,',');
		head_ptr->BookName=new char[strlen(s1.c_str())+1];
		strcpy(head_ptr->BookName,s1.c_str());
		getline(file,s2,',');
		head_ptr->Writer=new char[strlen(s2.c_str())+1];
		strcpy(head_ptr->Writer,s2.c_str());
		getline(file,s3,',');
		strcpy(head_ptr->PublishDate,&s3[0]);
		getline(file,s4,',');
		strcpy(head_ptr->ISBN,&s4[0]);
		getline(file,s5,',');
		strcpy(head_ptr->ebook,&s5[0]);
		getline(file,s6,',');
		strcpy(head_ptr->paperbook,&s6[0]);
		getline(file,s7,',');
		head_ptr->Publisher=new char[strlen(s7.c_str())+1];
		strcpy(head_ptr->Publisher,s7.c_str());
		getline(file,s8);
		head_ptr->BriefIntroduction=new char[strlen(s8.c_str())+1];
		strcpy(head_ptr->BriefIntroduction,s8.c_str());
		node = head_ptr;
		while(!file.eof())
		{
			p = new Book[sizeof(Book)];
			getline(file,s1,',');
			if(s1=="")//增强鲁棒性,如果文件被换行,最后一行为空,不加的话会最后一个节点错误
			{
				break;
			}
			p->BookName=new char[strlen(s1.c_str())+1];
			strcpy(p->BookName,s1.c_str());
			getline(file,s2,',');
			p->Writer=new char[strlen(s2.c_str())+1];
			strcpy(p->Writer,&s2[0]);
			getline(file,s3,',');
			strcpy(p->PublishDate,&s3[0]);
			getline(file,s4,',');
			strcpy(p->ISBN,&s4[0]);
			getline(file,s5,',');
			strcpy(p->ebook,&s5[0]);
			getline(file,s6,',');
			strcpy(p->paperbook,&s6[0]);
			getline(file,s7,',');
			p->Publisher=new char[strlen(s7.c_str())+1];
			strcpy(p->Publisher,&s7[0]);
			getline(file,s8,'\n');
			p->BriefIntroduction=new char[strlen(s8.c_str())+1];
			strcpy(p->BriefIntroduction,&s8[0]);
			p->next=NULL;
			node -> next= p; 
			node = p;
			// j++;
			n++;//书的数量
			//  cout<<"书的数量: "<<n<<endl;
		}
		file.close();
		delete p;
	}

总结:大量数据存储,每一个结构体分配的长度不相同,使用定长char浪费内存,使用动态空间的分配节约内存。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/*****************************************************************************************/#include #include #include #include //输入/输出文件流类using namespace std;const int Maxr=100;//最多的读者const int Maxb=100;//最多的图书const int Maxbor=5;//每位读者最多借五本书//读者类,实现对读者的信息的描述class Reader { private: int tag; //删除标记 1:已删 0:未删 int no; //读者编号 char name[10]; //读者姓名 int borbook[Maxbor];//所借图书 public: Reader() {} char *getname() {return name;} //获取姓名 int gettag() {return tag;} //获取删除标记 int getno() {return no;} //获取读者编号 void setname(char na[]) //设置姓名 { strcpy(name,na); } void delbook(){ tag=1; }//设置删除标记 1:已删 0:未删 void addreader(int n,char *na)//增加读者 { tag=0; no=n; strcpy(name,na); for(int i=0;i<Maxbor;i++) borbook[i]=0; } void borrowbook(int bookid)//借书操作 { for(int i=0;i<Maxbor;i++) { if (borbook[i]==0) { borbook[i]=bookid; return; } } } int retbook(int bookid)//还书操作 { for(int i=0;i<Maxbor;i++) { if(borbook[i]==bookid) { borbook[i]=0; return 1; } } return 0; } void disp()//读出读者信息 { cout << setw(5) << no <<setw(10) << name<<"借书编号:["; for(int i=0;i<Maxbor;i++) if(borbook[i]!=0) cout << borbook[i] << "|"; cout << "]"<<endl; }};//读者类库,实现建立读者的个人资料 class RDatabase{ private: int top; //读者记录指针 Reader read[Maxr];//读者记录public: RDatabase() //构造函数,将reader.txt读到read[]中 { Reader s; top=-1; fstream file("reader.txt",ios::in);//打开一个输入文件 while (1) { file.read((char *)&s,sizeof(s)); if (!file)break; top++; read[top]=s; } file.close(); //关闭 reader.txt } void clear()//删除所有读者信息 { top=-1; } int addreader(int n,char *na)//添加读者时先查找是否存在 { Reader *p=query(n); if (p==NULL) { top++; read[top].addreader(n,na); return 1; } return 0; } Reader *query(int readerid)//按编号查找 { for (int i=0;i<=top;i++) if (read[i].getno()==readerid && read[i].gettag()==0) { return &read[i]; } return NULL; } void disp() //输出所有读者信息 { for (int i=0;i<=top;i++) read[i].disp(); } void readerdata();//读者库维护 ~RDatabase() //析构函数,将read[]写到reader.txt文件中 { fstream file("reader.
具体功能要求 1、图书维护 (1)设置管理员账号和密码; (2)图书信息录入:图书编号、书名、作者名、分类、图书数量; (3)图书信息更改; (4)图书信息删除; (5)图书信息查询:按图书编号查询、按书名查询、按作者名查询; (6)图书信息全部显示; (7)图书信息全部删除; (8)退出图书维护界面。 2、读者维护 (1)设置管理员账号和密码; (2)读者信息录入:读者姓名、学号; (3)读者信息更改; (4)读者信息删除; (5)读者信息查询:按读者编号查询、按读者姓名查询; (6)读者信息全部显示; (7)读者信息全部删除; (8)退出读者维护界面。 3、借书 (1)设置借书的范围(如5本); (2)图书数量随着借书的数量减少; (3)输入读者编号与图书编号后借书成功。 4、还书 (1)图书数量随着还书的数量增加; (2)输入读者编号与图书编号后还书成功。 5、添加功能 (1)添加一本图书的基本信息,包括书名、图书编号、作者名、类别、图书数量。 (2)添加读者信息: 添加图书借阅的基本信息,包括书名、学号。 6、更改功能 对图书和读者的信息进行修改。 7、查找功能 (1)图书信息查找; (2)读者信息查找。 8、显示功能 (1)显示所有图书信息; (2)显示所有借阅信息。 9、删除功能 (1)删除图书的基本信息; (2)删除读者的基本信息; (3)删除图书借阅基本信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值