Unexplainable program crash

This is a discussion on Unexplainable program crash within the C++ Programming forums, part of the General Programming Boards category; Hi, I have three files - routeData.h, routeData.cpp, and main.cpp. Everything runs fine, but when I try to add another ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Unexplainable program crash

    Hi,

    I have three files - routeData.h, routeData.cpp, and main.cpp. Everything runs fine, but when I try to add another public int variable into routeData.h and set it's value in main.cpp to use in routeData.cpp, my program crashes as soon as it starts up - I can't figure out why! I seem to be doing the same thing with a different int variable that I declared in routeData.h and initiated in main.cpp. Here is my code:

    routeData.h
    Code:
    #ifndef ROUTE
    #define ROUTE
    #include <set>
    #include <list>
    
    using namespace std;
    
    typedef set<int>		    iSet;
    typedef list<int> 		    iLst;
    typedef set<int>::iterator	iSetPtr;
    typedef list<int>::iterator	iLstPtr;
    
    class routeData
    {
        public:
    	
        	int readRouteData(istream & inSource);
        
        	int distance(int first, int second);
        
        	int distance(iLst route);
        
        	iLst bestRoute(iLst done, iSet left);
    
            int **myLocations;
            int totalLocations;
            int shortestDistance; // problem with this var
            iLst finalRoute;
            iLst holder;
            iSet locs;
    };
    
    #endif
    routeData.cpp
    Code:
    #include <iostream>
    #include <set>
    #include <list>
    #include <cstdlib>
    #include <string>
    #include <sstream>
    #include <ctype.h>
    #include "routeData.h"
    
    using namespace std;
    
    iLst routeData::bestRoute(iLst done, iSet left)
    {
        int setSize;
        iSetPtr setIterator;
        iSetPtr tempPtr;
        iLst finalList;
    
        setSize = left.size();
    
        if(setSize == 0)
        {
            cout << "PERMUTATION SEQUENCE FINISHED.\n";
            cout << "->Distance of final permutation: ";
            cout << distance(done) << "\n\n";
            cout << "Comparison: distance of done = " << distance(done) << ", distance of shortestDistance = " << shortestDistance << "\n";
            if((distance(done) < shortestDistance) || (shortestDistance == -1))
            {
                cout << "Distance of done is < shortestDistance\n";
                shortestDistance = distance(done);
            }
            cout << "SHORTEST CURRENT DISTANCE: " << shortestDistance << "\n";
        }
    
                
        for(setIterator = left.begin(); setIterator != left.end(); setIterator++)
        {
            cout << "Iterator: " << *setIterator << "\n";
    
            iLst tempList = done;
            iSet tempSet = left;
    
            tempSet.erase(*setIterator);
            tempList.push_back(*setIterator);
    
            iLstPtr tempLstPtr;
            cout << "[ ";
            for(tempLstPtr = tempList.begin(); tempLstPtr != tempList.end(); tempLstPtr++)
            {
                cout << *tempLstPtr << " ";
            }
            cout << "]\t";
    
            iSetPtr tempPtr;
            cout << "[ ";
            for(tempPtr = tempSet.begin(); tempPtr != tempSet.end(); tempPtr++)
            {
                cout << *tempPtr << " ";
            }
            cout << "]\n";
            
    
            cout << "Size of list: " << tempList.size() << "\n";
            cout << "Size of set: " << tempSet.size() << "\n";
            cout << "Total distance of this list: " << distance(tempList) << "\n\n";
                    
            bestRoute(tempList, tempSet);
                
        }
       
        
        return done;
    }
    main.cpp
    Code:
    #include <iostream>
    #include <set>
    #include <list>
    #include <cstdlib>
    #include "routeData.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        routeData myRoute;
        int rrdResult;
    
        myRoute.myLocations = new int*[myRoute.totalLocations];
    
        for(int i = 0; i < 10; i++)
        {
            myRoute.myLocations[i] = new int[myRoute.totalLocations];
        }
    
        rrdResult = myRoute.readRouteData(cin);
    
        if(rrdResult < 0)
        {
            cerr << "Bad input on line " << rrdResult;
            exit(1);
        }
    
        for(int i = 1; i <= myRoute.totalLocations; i++)
        {
            if(i == 1)
            {
                myRoute.holder.push_back(i);
            }
            else
            {
                myRoute.locs.insert(i);
            }
        }
    
        myRoute.shortestDistance = -1;
    
        myRoute.finalRoute = myRoute.bestRoute(myRoute.holder, myRoute.locs);
    
        cout << "----------------------------------\n";
        cout << "DISTANCE OF LAST RETURNED ROUTE: ";
        cout << myRoute.distance(myRoute.finalRoute) << "\n\n";
    
        system("Pause");
        return 0;
    }
    I have no idea why this makes my program crash. All it says is:

    This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
    Any ideas?
    Last edited by Beowolf; 10-31-2007 at 02:35 AM.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The first problem I see is on the third line of main, where you use the uninitialized totalRoutes as an argument to array new. You do that again three lines down.

    Fix those (and all the other uninitialized variables) and come back.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 11:26 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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