views:

18

answers:

1
void BinaryTree::InitializeFromFile(string Filename){
 ifstream inFile;
 treenode* Freq[256];
 inFile.open(Filename.c_str(), fstream::binary);
 if(inFile.fail()){
  cout<<"Error in opening file "<<Filename;
  return;
 }
 for(int i=0;i<=255;i++){ 
  Freq[i]->weight=0; 
  Freq[i]->data = '0'+i; 
  Freq[i]->LChild = NULL; Freq[i]->RChild=NULL; Freq[i]->Parent=NULL;
 }
 char c;
 inFile.get(c);
 while(!inFile.eof()){
  Freq[c]->weight ++;
  inFile.get(c);
 }
}

I'm getting the Access Violation Exception in the for loop. Even when I comment out certain lines it'll give me an error on the next line in that loop.

Edit: Also is the line Freq[c]->weight ++; valid? Can I go to a specific part of the array based on the char value?

+1  A: 

You seem to never initialize your Freq table. It contains random pointers. Dereferncing an uninitialized pointer leads to undefined behaviour.

You ought to add Freq[i] = new treenode before Freq[i]->weight=0;.

Vlad
*Facepalm* Thank you, I'm surprised I forgot to initialize it. Any answer to my edit?
Azreal
I've read one day Don Knuth's text describing the stupid mistakes he did during development of TeX; people are just people, everyone makes mistakes. About your edit: `Freq[c]->weight ++` seems to be perfectly OK.
Vlad