内存问题,小弟跪求解答!!!

didawanggang 2010-12-20 08:10:05
程序运行是弹出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;
}
/**/

}
...全文
325 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
cwbcwb505 2010-12-21
  • 打赏
  • 举报
回复
string FileTempName = FileTempName + ".temp";
这是什么意思啊?我日!!
luciferisnotsatan 2010-12-21
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 gules 的回复:]

std::bad_alloc是C++标准库定义的导常类型,一般是调用new或new[]时抛出;
但用搜索的方法却在楼主代码中找不到任何new的代码,不解!(贴出代码不全还是修改过?)
[/Quote]
+1

从lz的代码,应该不会出现这个问题。
didawanggang 2010-12-21
  • 打赏
  • 举报
回复
嗯,刚才优化了下,
string FileTempName = FileTempName + ".temp";
最好写成string::append(const char *)的形式。
C++果然博大精深啊。各位以后在字符串的处理上一定要留个心眼哇
didawanggang 2010-12-21
  • 打赏
  • 举报
回复
各位大哥,小弟解决了,主要原因就在于string的重载 ‘+’上,我反汇编搞出来的,调试了半夜,只加一个头文件引用就ok了。
#include <cstring>
谢了,那个,再来几个人回复下,我结贴。
gules 2010-12-20
  • 打赏
  • 举报
回复
std::bad_alloc是C++标准库定义的导常类型,一般是调用new或new[]时抛出;
但用搜索的方法却在楼主代码中找不到任何new的代码,不解!(贴出代码不全还是修改过?)
花熊 2010-12-20
  • 打赏
  • 举报
回复
didawanggang 2010-12-20
  • 打赏
  • 举报
回复
回9楼,主函数引用
#include "Compress.h"
using namespace std;


void main()
{

Compress("read.txt");
}
hs512 2010-12-20
  • 打赏
  • 举报
回复
有一种东西叫调试(debug)~~
zy020118 2010-12-20
  • 打赏
  • 举报
回复
粘了这么多,还没有主函数,让人怎么看呢。。。
didawanggang 2010-12-20
  • 打赏
  • 举报
回复
回7楼jsjygm
我的编译器是vs2005;
我编译的时候没有错误。运行的时候抛出异常。
jsjygm 2010-12-20
  • 打赏
  • 举报
回复
我运行的不是这问题呀,怎么有语法错误,

E:\test\15.cpp(167) : error C2668: 'insert' : ambiguous call to overloaded function
my1111ym 2010-12-20
  • 打赏
  • 举报
回复
又是一个不会粘代码的……
hs512 2010-12-20
  • 打赏
  • 举报
回复
是说申请的空间超过了系统限制,Invalid allocation size - CCCCCCF1 (exceeded 7ffdefff)
didawanggang 2010-12-20
  • 打赏
  • 举报
回复
HaffmanConcode.exe 中的 0x7c812afb 处最可能的异常: Microsoft C++ 异常: 内存位置 0x0012f91c 处的 std::bad_alloc。
HEAP[HaffmanConcode.exe]: Invalid allocation size - CCCCCCF1 (exceeded 7ffdefff)
HaffmanConcode.exe 中的 0x7c812afb 处最可能的异常: Microsoft C++ 异常: 内存位置 0x0012f280 处的 std::bad_alloc。
HaffmanConcode.exe 中的 0x7c812afb 处最可能的异常: Microsoft C++ 异常: 内存位置 0x00000000 处的 [rethrow]。
HaffmanConcode.exe 中的 0x7c812afb 处未处理的异常: Microsoft C++ 异常: 内存位置 0x0012f280 处的 std::bad_alloc。

么意思?
didawanggang 2010-12-20
  • 打赏
  • 举报
回复
回一楼,试过了,没用。
didawanggang 2010-12-20
  • 打赏
  • 举报
回复
兄弟们啊。在线等呢,来个人给俺指点下撒。。。
hs512 2010-12-20
  • 打赏
  • 举报
回复
int FileCount;未初始化
HtreeCount = 2 * FileCount - 1;有可能是负数
HaffStruct *Htree = (HaffStruct *)malloc(sizeof(HaffStruct) * HtreeCount);

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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