-
Close - But a Crash
Hi. I just wrote the traverse function. No compile-time errors. The program crashes at run-time. Any idea why? I thought it should work. Thanks always, Steve
Code:
#include<iostream>
#include<vector>
#include<fstream>
#include<stack>
using namespace std;
struct Huff;
void deletePtr(vector<Huff*>& v, Huff* m);
Huff* findMinimum(vector<Huff*>& v);
void traverse(vector<Huff*>& v);
struct Huff
{
Huff();
int count;
char ch;
Huff* left;
Huff* right;
Huff* parent;
};
int main(int argc, char* argv[])
{
if(argc<2)
{
cout<<"Not enough arguments were provided."<<endl;
return -1;
}
ifstream myFile(argv[1], ios::in);
if(!myFile.is_open())
{
cout<<"Unknown file specified."<<endl;
return -1;
}
char cz;
int i;
vector<Huff*> values;
while(myFile.get(cz))
{
if(cz < 'a' || cz > 'z')
continue;
for(i=0; i<values.size(); i++)
{
if(values[i]->ch == cz)
{
values[i]->count++;
break;
}
}
if(values.size()==i)
{
Huff* h = new Huff;
h->ch=cz;
h->count=1;
values.push_back(h);
}
}
if(values.size() == 0)
{
cout<<"File is empty.\n";
return 0;
}
if(values.size() == 1)
{
cout<<values[0]->ch<<"1"<<endl;
}
vector<Huff*> copy;
for(i=0; i<values.size(); i++)
{
copy.push_back(values[i]);
}
Huff* min;
Huff* min2;
while(values.size() > 1)
{
min=findMinimum(values);
deletePtr(values, min);
min2=findMinimum(values);
deletePtr(values, min2);
Huff* p=new Huff;
p->left=min;
p->right=min2;
p->count=min->count+min2->count;
values.push_back(p);
}
traverse(copy);
return 0;
}
Huff::Huff() : ch(0), count(0) { }
Huff* findMinimum(vector<Huff*>& v)
{
int i, minCo=v[0]->count;
char minCh=v[0]->ch;
Huff* h3=v[0];
for(i=1; i<v.size(); i++)
{
if(v[i]->count < minCo)
{
minCo=v[i]->count;
minCh=v[i]->ch;
h3=v[i];
}
}
return h3;
}
void deletePtr(vector<Huff*>& v, Huff* m)
{
int i;
i=0;
while(i < v.size() && v[i]->ch != m->ch)
{
i++;
}
for( ; i<v.size()-1; i++)
{
v[i]=v[i+1];
}
v.pop_back();
}
void traverse(vector<Huff*>& v)
{
int i;
Huff* p;
stack<int> s;
for(i=0; i<v.size(); i++)
{
p=v[i];
while(p->parent != NULL)
{
p=p->parent;
if(p->left->ch == v[i]->ch)
{
s.push(0);
}
else
{
s.push(1);
}
}
cout<<v[i]->ch<<" ";
while(!s.empty())
{
cout<<s.top();
s.pop();
}
}
}
-
The traverse method uses parent, but nothing ever sets the parent pointer in the new Huff objects you create, and your Huff constructor does not initialize them to NULL.
You also never actually delete the memory in your deletePtr method.
Don't know if either of those are causing your specific crash, but they should probably be fixed.
-
When I run it, it doesn't crash it just exits out because in your if statements you return a value so it thinks the main function is over. Add system("PAUSE"); before all your return statements to see which if statements are being called.
-
Hi. Thanks jlou ("hi again"), and terran9. I will try what you have said. terran9, just so you are aware, this program is meant to run from the command line. It does this fine. I could use debugging methods to do what you have suggested in that respect (from the CL). Using system("pause") is not necessary, as this would bring execution back into my IDE, which is not what I'm looking to do. Also, I think you may be getting "returned" at the wrong place, though I could be mistaken. Thanks anyway though. Regards, Steve