Close - But a Crash

This is a discussion on Close - But a Crash within the C++ Programming forums, part of the General Programming Boards category; Hi. I just wrote the traverse function. No compile-time errors. The program crashes at run-time. Any idea why? I thought ...

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    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();
            }
        }
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    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.

  4. #4
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  2. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  3. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  4. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21