新手!大神请进!关于写入位置 0xCDCDCDCD 时发生访问冲突
新手求助!大神请进!关于写入位置 0xCDCDCDCD 时发生访问冲突
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
class tree{
public:
char data;
int weight;
tree *left,*right,*parent;
tree():data(0){};
};
class htree{
public:
int leafnum;//叶结点数
int nodenum;//总结点数
tree **nodes;//nodes数组
char **huffcode;//huffmancode数组
htree(){};//构造函数
void init_htree(int *weights,int leafn,htree &treeh);//初始化
void create_htree(htree &treeh);//构造
void encode(htree &treeh);//编码
void print(htree &treeh);//输出
};
void htree::init_htree(int *weights,int leafn,htree &treeh){
leafnum=leafn;
nodenum=2*leafnum-1;
nodes=new tree*[nodenum];
huffcode=new char*[leafnum];
for(int i=0;i<leafnum;i++){
memset(huffcode[i],0,leafnum+1);
}
for(int i=0;i<nodenum;i++){
if(i<leafnum)
nodes[i]->weight=weights[i];
else
nodes[i]->weight=0;
nodes[i]->parent=NULL;
nodes[i]->left=NULL;
nodes[i]->right=NULL;
nodes[i]->parent->weight=0;
nodes[i]->left->weight=0;
nodes[i]->right->weight=0;
}
}
void htree::create_htree(htree &treeh){
int min1=10000,min2=10000;//min1存放最小值,min2存放第二小的值
int leftc=0,rightc=0;
for(int i=leafnum;i<nodenum;i++){
for(int j=0;j<leafnum;j++){
if(nodes[j]->parent==NULL){
if(nodes[j]->weight<min1){
min2=min1;
rightc=leftc;
min1=nodes[j]->weight;
leftc=j;}
else if(nodes[j]->weight<min2){
min2=nodes[j]->weight;
rightc=j;}
}
}
nodes[leftc]->parent=nodes[i];
nodes[rightc]->parent=nodes[i];
nodes[i]->left=nodes[leftc];
nodes[i]->right=nodes[rightc];
nodes[i]->weight=min1+min2;
min1=10000,min2=10000;
leftc=0;rightc=0;
}
encode(treeh);
}
void htree::encode(htree &treeh){
char* code_temp=new char[leafnum+1];
memset(code_temp,0,leafnum+1);
int start=leafnum;
for(int i=0;i<leafnum;i++){
tree* cur_node=nodes[i];
tree* cur_parent=nodes[i]->parent;
while(cur_parent!=NULL){
if(cur_parent->left==cur_node){
code_temp[--start]='0';}
else if(cur_parent->right==cur_node){
code_temp[--start]='1';}
cur_node=cur_parent;
cur_parent=cur_parent->parent;
}
memcpy(huffcode[i],(code_temp+start),(leafnum-start+1));
memset(code_temp,0,leafnum+1);
start=leafnum;
}
delete [] code_temp;
}
void htree::print(htree &treeh){
for(int i=0;i<leafnum;i++){
cout<<huffcode[i]<<endl;
}
}
void main(){
ofstream fout("inputfile1.txt");
fout<<"Indeed, as I learned, there were on the planet where the little prince lived-- as on all planets-- good plants and bad plants. In consequence, there were good seeds from good plants, and bad seeds from bad plants. But seeds are invisible. They sleep deep in the heart of the earth's darkness, until some one among them is seized with the desire to awaken. Then this little seed will stretch itself and begin-- timidly at first-- to push a charming little sprig inoffensively upward toward the sun. If it is only a sprout of radish or the sprig of a rose-bush, one would let it grow wherever it might wish. But when it is a bad plant, one must destroy it as soon as possible, the very first instant that one recognizes it.";
fout.close();
ifstream fin("inputfile1.txt");
int array[26]={0};
char ch;
int i=0;
fin.get(ch);
if(ch>='a'&&ch<='z'){
array[ch-'a']++;
}
if(ch>='A'&&ch<='Z'){
array[ch-'A']++;
}
while(!fin.eof()){
cout<<ch;
i++;
fin.get(ch);
if(ch>='a'&&ch<='z'){
array[ch-'a']++;
}
if(ch>='A'&&ch<='Z'){
array[ch-'A']++;
}
}
cout<<endl<<"len="<<i<<endl;
fin.close();
for(int i=0;i<26;i++){
cout<<array[i]<<endl;
}
htree hufftree;
hufftree.init_htree(array,26,hufftree);
hufftree.create_htree(hufftree);
hufftree.print(hufftree);
}
显示0xcdcdcdcd发生访问冲突 我知道可能是指针未初始化导致的 但是不知道是哪儿出了问题 求大神帮忙解答
谢谢
------解决思路----------------------
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
------解决思路----------------------
你没有给 huffcode[i] 分配空间。
------解决思路----------------------
huffcode就char类型的指针,huffcode[i]只是char数组的第i个字符,
只占一字节,怎么可能存储leafnum+1个0呢
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
class tree{
public:
char data;
int weight;
tree *left,*right,*parent;
tree():data(0){};
};
class htree{
public:
int leafnum;//叶结点数
int nodenum;//总结点数
tree **nodes;//nodes数组
char **huffcode;//huffmancode数组
htree(){};//构造函数
void init_htree(int *weights,int leafn,htree &treeh);//初始化
void create_htree(htree &treeh);//构造
void encode(htree &treeh);//编码
void print(htree &treeh);//输出
};
void htree::init_htree(int *weights,int leafn,htree &treeh){
leafnum=leafn;
nodenum=2*leafnum-1;
nodes=new tree*[nodenum];
huffcode=new char*[leafnum];
for(int i=0;i<leafnum;i++){
memset(huffcode[i],0,leafnum+1);
}
for(int i=0;i<nodenum;i++){
if(i<leafnum)
nodes[i]->weight=weights[i];
else
nodes[i]->weight=0;
nodes[i]->parent=NULL;
nodes[i]->left=NULL;
nodes[i]->right=NULL;
nodes[i]->parent->weight=0;
nodes[i]->left->weight=0;
nodes[i]->right->weight=0;
}
}
void htree::create_htree(htree &treeh){
int min1=10000,min2=10000;//min1存放最小值,min2存放第二小的值
int leftc=0,rightc=0;
for(int i=leafnum;i<nodenum;i++){
for(int j=0;j<leafnum;j++){
if(nodes[j]->parent==NULL){
if(nodes[j]->weight<min1){
min2=min1;
rightc=leftc;
min1=nodes[j]->weight;
leftc=j;}
else if(nodes[j]->weight<min2){
min2=nodes[j]->weight;
rightc=j;}
}
}
nodes[leftc]->parent=nodes[i];
nodes[rightc]->parent=nodes[i];
nodes[i]->left=nodes[leftc];
nodes[i]->right=nodes[rightc];
nodes[i]->weight=min1+min2;
min1=10000,min2=10000;
leftc=0;rightc=0;
}
encode(treeh);
}
void htree::encode(htree &treeh){
char* code_temp=new char[leafnum+1];
memset(code_temp,0,leafnum+1);
int start=leafnum;
for(int i=0;i<leafnum;i++){
tree* cur_node=nodes[i];
tree* cur_parent=nodes[i]->parent;
while(cur_parent!=NULL){
if(cur_parent->left==cur_node){
code_temp[--start]='0';}
else if(cur_parent->right==cur_node){
code_temp[--start]='1';}
cur_node=cur_parent;
cur_parent=cur_parent->parent;
}
memcpy(huffcode[i],(code_temp+start),(leafnum-start+1));
memset(code_temp,0,leafnum+1);
start=leafnum;
}
delete [] code_temp;
}
void htree::print(htree &treeh){
for(int i=0;i<leafnum;i++){
cout<<huffcode[i]<<endl;
}
}
void main(){
ofstream fout("inputfile1.txt");
fout<<"Indeed, as I learned, there were on the planet where the little prince lived-- as on all planets-- good plants and bad plants. In consequence, there were good seeds from good plants, and bad seeds from bad plants. But seeds are invisible. They sleep deep in the heart of the earth's darkness, until some one among them is seized with the desire to awaken. Then this little seed will stretch itself and begin-- timidly at first-- to push a charming little sprig inoffensively upward toward the sun. If it is only a sprout of radish or the sprig of a rose-bush, one would let it grow wherever it might wish. But when it is a bad plant, one must destroy it as soon as possible, the very first instant that one recognizes it.";
fout.close();
ifstream fin("inputfile1.txt");
int array[26]={0};
char ch;
int i=0;
fin.get(ch);
if(ch>='a'&&ch<='z'){
array[ch-'a']++;
}
if(ch>='A'&&ch<='Z'){
array[ch-'A']++;
}
while(!fin.eof()){
cout<<ch;
i++;
fin.get(ch);
if(ch>='a'&&ch<='z'){
array[ch-'a']++;
}
if(ch>='A'&&ch<='Z'){
array[ch-'A']++;
}
}
cout<<endl<<"len="<<i<<endl;
fin.close();
for(int i=0;i<26;i++){
cout<<array[i]<<endl;
}
htree hufftree;
hufftree.init_htree(array,26,hufftree);
hufftree.create_htree(hufftree);
hufftree.print(hufftree);
}
显示0xcdcdcdcd发生访问冲突 我知道可能是指针未初始化导致的 但是不知道是哪儿出了问题 求大神帮忙解答
谢谢
------解决思路----------------------
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
------解决思路----------------------
你没有给 huffcode[i] 分配空间。
------解决思路----------------------
for(int i=0;i<leafnum;i++){
memset(huffcode[i],0,leafnum+1);
}
这儿不对。
下断点,单步跟踪,很容易就找到出错的地方了。
我找到是这儿的错误了,我想问是为什么不对...谢谢
huffcode就char类型的指针,huffcode[i]只是char数组的第i个字符,
只占一字节,怎么可能存储leafnum+1个0呢