内存问题,小弟跪求解答!!!
程序运行是弹出std::bad_alloc异常;
兄弟们,给个办法吧,烦死了!
代码如下,
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
#define FIleMaxNum 127
#define MaxValue 65530
///////////////////////////////////////////////////
//FileData:
char FileElemC[FIleMaxNum];
int FileElemP[FIleMaxNum];
string FileRepElemStr[FIleMaxNum];
//Haffman Data:
typedef struct{
int weight;
int parent;
int lchild;
int rchild;
int flags;
}HaffStruct;
///////////////////////////////////////////////////
bool Compress(string strPath) // 压缩
{
/////////////////////////////////////////
//Data Of Function;
int FileCount;
int HtreeCount;
string FIleName = "e:\\read.txt"; //测试数据->路径2010.12.20;
//FIleName = Path;
string FileTempName = FileTempName + ".temp";
/////////////////////////////////////////
//Code of Function
ifstream rf;
rf.open(FIleName.c_str());
if (!rf)
{
cout << "Rf不能打开!\n";
return false;
}
char c;
int tag = 0;
// 扫描获取File信息
while( !rf.eof())
{
rf.get(c);
for (int x = 0; x < FileCount; x++)
{
if (c == FileElemC[x])
{
tag = 1;
FileElemP[x] += 1;
break;
}
if (FileCount >= 2 && x < FileCount - 1 && FileElemP[x] < FileElemP[x + 1] )
{
char ctemp = FileElemC[x];
long ltemp = FileElemP[x];
FileElemP[x] = FileElemP[x + 1];
FileElemC[x] = FileElemC[x + 1];
FileElemP[x + 1] = ltemp;
FileElemC[x + 1] = ctemp;
}
}
if (tag != 1)
{
FileElemC[FileCount] = c;
FileElemP[FileCount] += 1;
FileCount++;
}
tag = 0;
}
rf.close();
//-------------------------------------------------------
//Haffman Code:
HtreeCount = 2 * FileCount - 1;
HaffStruct *Htree = (HaffStruct *)malloc(sizeof(HaffStruct) * HtreeCount);
//FileStruct *fData = (FileStruct *)malloc(sizeof(FileStruct) * FileCount);
//HaffStuct Htree[255];
/* Haffman Init() */
for (int x = 0; x < HtreeCount;x++)
{
if (x < FileCount)
{
Htree[x].weight = FileElemP[x];
}
else
{
Htree[x].weight = 0;
}
/* end of init Htree[].weight */
Htree[x].parent = -1;
Htree[x].flags = 0;
Htree[x].lchild = -1;
Htree[x].rchild = -1;
/* end of othes Elem */
}
/* 构造haffman 的其他的非叶子节点 */
int m1,m2;
int x1,x2;
for (int x = 0; x < FileCount - 1; x ++)
{
m1 = m2 = MaxValue;
x1 = x2 = 0;
for (int y = 0; y < FileCount + x; y ++)
{
if (Htree[y].weight < m1 && Htree[y].flags == 0)
{
m2 = m1;
x2 = x1;
m1 = Htree[x].weight;
x1 = y;
}
else if (Htree[y].weight < m2 && Htree[y].flags == 0)
{
m2 = Htree[y].weight;
x2 = y;
}
}
/* 查找两个最小子树结束 */
/* 合并子树beg */
Htree[x1].flags= 1;
Htree[x2].flags = 1;
Htree[x1].parent = FileCount + x;
Htree[x2].parent = FileCount + x;
Htree[FileCount + x].weight = Htree[x1].weight + Htree[x2].weight;
Htree[FileCount + x].lchild = x1;
Htree[FileCount + x].rchild = x2;
}
/* 记录码字 */
int _child,_parent;
for (int x = 0; x < FileCount; x++)
{
_child = x;
_parent = Htree[_child].parent;
while(_parent != -1)
{
if (Htree[_parent].lchild == _child)
{
FileRepElemStr[x].insert(0,1,'0');
/**
int _i = fData[x].flagsbit;
fData[x].bit[_i] = 0;
fData[x].flagsbit++;
/**/
}
else
{
FileRepElemStr[x].insert(0,1,'1');
/**
int _i = fData[x].flagsbit;
fData[x].bit[_i] = 0;
fData[x].flagsbit++;
*/
}
_child = _parent;
_parent = Htree[_child].parent;
}
}
ofstream wTempfile;
wTempfile.open(FileTempName.c_str());
ifstream rfile;
rfile.open(FIleName.c_str());
rfile.seekg(0,ios::beg);
while( !rfile.eof())
{
rf.get(c);
for (int x = 0; x < FileCount; x++)
{
if (FileElemC[x] == c)
{
wTempfile << FileRepElemStr[x];
}
break;
}
}
long len_TempTxt = wTempfile.tellp();
rfile.close();
wTempfile.close();
///////////////////////////////////////////////
//TEST CODE OF FUNCTION
for (int x = 0; x < FileCount; x++)
{
cout << FileElemC[x] << " " << FileElemP[x] << " "<< FileRepElemStr[x] << endl;
}
/**/
}