爆内存的问题
while (getline(file, line))读一个几十MB的记事本,再放到
string buf_A[4098]表里,一直循环读,会爆内存。读完后要怎样才能释放内存。
https://img-mid.csdnimg.cn/release/static/image/mid/ask/c2e6e63d86ea4c949043f20c645b2f43.png
代码如下:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <string.h>
#include <time.h>
#include "windows.h"
#include <direct.h>
#include <sys/stat.h>
using namespace std;
//******************************************定义全局变量***********************************************//
string buf_A[4098]; //写数据表缓冲x*6 1024*4+2=4096+2=4098 表B大空间可以大大增加命中率
char i = 0;
int bufB_num = 0; //单数总和
long int all_num = 0; //总数
long int vaild_files = 0; //有效文件
//*****************************************************************************************************************************************//
void Exedat() //查检路径打开文件在不在+处理数据
{
char* logpath = "C:\\Users\\work\\Documents\\visual studio 2017\\Projects\\ConsoleApplication1\\Debug\\09.log";
ofstream outfile;
outfile.open(logpath, ios::out | ios::app);
fstream file("D:\\Desktop VS\\新建文件夹\\00A-37MB.txt", ios::in);
if (!file)
{
cout << "Sorry Failed to open file!" << endl;
file.close(); //关闭读文件流
system("pause");
exit(0); //直接结束
}
cout << "scaning . . . dat please waiting . . . !" << endl; //显示一下开始扫描
string line;
int countA = 0;
while (getline(file, line))
{
buf_A[countA] = line;
all_num++;
countA++;
if (countA >= 4096) //表A上限4098 如果超值清空数组
{
countA = 0; //清零
memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
}
}
outfile.close(); //关闭写文件流
//******************************输出************************************//
cout << "all line count = " << "< " << all_num << " >" << endl;
cout << "filter file count = " << "< " << vaild_files << " >" << endl;
//*********************清空数组***********************//
countA = 0;
memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
all_num = 0; //只能在下面清空总数
vaild_files = 0;
line = "";
//****************************************************//
file.close(); //关闭读文件流
}
//************************************************************************************************//
int main()
{
while (1)
{
Exedat();
system("pause");
}
}
