Help with memory problems!

This is a discussion on Help with memory problems! within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm wondering, when I make a class in c++, I have lots of variables, and after I run my ...

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    Help with memory problems!

    Hi! I'm wondering, when I make a class in c++, I have lots of variables, and after I run my code a bunch of times, my computer freezes(it's an old one). I think it has something to do with freeing up the space in the computers memory, but I don't know how to do that. Could you please help! Thanks!

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Are you allocating the memory for them dynamically, using the new keyword or malloc()? If so, be sure to use the delete keyword on everything you use new for, and use free() on all pointers created with malloc().

    (new and delete are the C++ method, malloc and free are used in C, but also work in C++)

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Unless we are talking massive recursion here...

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Good point, though you would probably run into problems with that more often than the OP is implying. Care to post some source code?

  5. #5
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    I just used normal variables like
    Code:
    int hello;

  6. #6
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Its a little long
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    int choice;
    int quantity;
    
    class sector
    {
    private:
      unsigned int credits;
      unsigned int power;
      unsigned int metal;
      unsigned int crew;
      unsigned int jets;
      unsigned int starships;
      unsigned int days;
      unsigned int jettime[1];
      unsigned int startime[1];
      unsigned int minetime[1];
      unsigned int traintime[1];
    public:
      sector();
      int timer();
      int retrieve(int);
      int build();
      int mine();
      int train();
      int stats();
      int next();
      ~sector();
    };
    sector::sector()                                                    //Constructor
        {
          credits = 1000000;
          power = 100;
          metal = 100;
          crew = 50;
          jets = 0;
          starships = 1;
          days = 1;
          jettime[0] = 0;
          startime[0] = 0;
          minetime[0] = 0;
          traintime[0] = 0;
          jettime[1] = 0;
          startime[1] = 0;
          minetime[1] = 0;
          traintime[1] = 0;
        }
    
    int sector::timer()                                                  //TIMER
        {
          system("cls");
          days++;
          power = power + 20;
          if (jettime[0] == days)
              {
                jets = jets + jettime[1];
                cout<<"Alas, your jets are complete!"<<endl;
                jettime[0] = 0;
                jettime[1] = 0;
              }
          if (startime[0] == days)
             {
               starships = starships + startime[1];
               cout<<"Alas, your StarShips are complete!"<<endl;
               startime[0] = 0;
               startime[1] = 0;
             }
          if (minetime[0] == days)
             {
               metal = metal + (minetime[1]* 10);
               cout<<"Alas, your mining team has returned!"<<endl;
               minetime[0] = 0;
               minetime[1] = 0;
             }
          if (traintime[0] == days)
             {
               crew = crew + traintime[1];
               cout<<"Alas, your crew training is complete!"<<endl;
               traintime[0] = 0;
               traintime[1] = 0;
             }
          system("PAUSE");
          return 0;
        }
    
    int sector::build()                                         //BUILD
        {
          if ((jettime[1] != 0) || (startime[1] != 0))
             {return 0;}
          else
          quantity = 0;
          int cred;
          int pow;
          int met;
          int cre;
          cred = 0;
          pow = 0;
          met = 0;
          cre = 0;
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\nWhat are your building orders, Admiral?"<<endl;
          cout<<"\n[1] Jets"
              <<"\n[2] StarShips"
              <<"\n[0] Back\n"<<endl;
          cout<<"Order: ";
          cin>>choice;
          cin.ignore();
          if (choice == 1)
             {
               cout<<"\nThis costs 100 credits, 10 power, 10 metal, and 2 crew. Quantity: ";
               cin>>quantity;
               cin.ignore();
               cred = quantity * 100;
               pow = quantity * 10;
               met = quantity * 10;
               cre = quantity * 2;
               if ((retrieve(1) >= cred) && (retrieve(2) >= pow) && (retrieve(3) >= met) && (retrieve(4) >= cre))
                   {
                    cout<<"\n\n SUCCESS! Your ships will be built in "<<(quantity * 1)<<" days.\n"<<endl;
                    system("PAUSE");
                    credits = credits - cred;
                    power = power - pow;
                    metal = metal - met;
                    crew = crew - cre;
                    jettime[0] = days + (quantity * 1);
                    jettime[1] = quantity;
                    return 0;
                   }
                else
                   {
                    cout<<"\n\nNOT ENOUGH SUPPLIES! Cannot build.\n"<<endl;
                    system("PAUSE");
                    return 0;
                   }
             }
          else if (choice == 2)
             {
               cout<<"\nThis costs 1000 credits, 50 power, 50 metal, and 8 crew. Quantity: ";
               cin>>quantity;
               cin.ignore();
               cred = quantity * 1000;
               pow = quantity * 50;
               met = quantity * 50;
               cre = quantity * 8;
               if ((retrieve(1) >= cred) && (retrieve(2) >= pow) && (retrieve(3) >= met) && (retrieve(4) >= cre))
                   {
                    cout<<"\n\n SUCCESS! Your ships will be built in "<<(quantity * 5)<<" days.\n"<<endl;
                    system("PAUSE");
                    credits = credits - cred;
                    power = power - pow;
                    metal = metal - met;
                    crew = crew - cre;
                    startime[0] = days + (quantity * 5);
                    startime[1] = quantity;
                    return 0;
                   }
                else
                   {
                    cout<<"\n\nNOT ENOUGH SUPPLIES! Cannot build.\n"<<endl;
                    system("PAUSE");
                    return 0;
                   }
             }
          else
             {return 0;}
        }
    
    int sector::mine()
        {
          if (minetime[1] != 0)
             {return 0;}
          else
          quantity = 0;
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\n\nShall we mine, Admiral?"<<endl;
          cout<<"\n   How many crew members will you send? ";
          cin>>quantity;
          cin.ignore();
          if (quantity > retrieve(4))
              {
                cout<<"\n\nNOT ENOUGH CREW! Cannot send.\n"<<endl;
                system("PAUSE");
                return 0;
              }
          else if (quantity == 0)
              {return 0;}
          else
              {
                cout<<"\n\nSUCCESS! "<<quantity<<" crew members have been sent to mine.\n"
                    <<"They will be back in 4 days.\n"<<endl;
                system("PAUSE");
                crew = crew - quantity;
                minetime[0] = days + 4;
                minetime[1] = quantity;
                return 0;
              }
        }
    
    int sector::train()
        {
           if (traintime[1] != 0)
             {return 0;}
          else
          quantity = 0;
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\n\nShall we train the crew, Admiral?"<<endl;
          cout<<"\n   How many men do you want to train into crew($10 each)? ";
          cin>>quantity;
          cin.ignore();
          if ((quantity * 10) > retrieve(1))
              {
                cout<<"\n\nNOT ENOUGH CREDITS! Cannot train.\n"<<endl;
                system("PAUSE");
                return 0;
              }
          else if (quantity == 0)
              {return 0;}
          else
              {
                cout<<"\n\nSUCCESS! "<<quantity<<" crew members are being trained.\n"
                    <<"They will be ready in "<<(quantity / 10)<<" days."<<endl;
                system("PAUSE");
                credits = credits - (quantity * 10);
                traintime[0] = days + (quantity / 10);
                traintime[1] = quantity;
                return 0;
              }
        }
    
    int sector::stats()
        {
          system("cls");
          cout<<"Credits: "<<retrieve(1)<<setw(15)<<"Power: "<<retrieve(2)<<setw(15)<<"Metal: "<<retrieve(3)<<setw(15)<<"Day "<<retrieve(7)<<endl;
          cout<<"Crew: "<<retrieve(4)<<setw(15)<<"Jets: "<<retrieve(5)<<setw(15)<<"Starships: "<<retrieve(6)<<endl;
          cout<<"\n\n  Jets in Production | StarShips in Production | Miners Gone | Crew in Training"<<endl;
          cout<<"-------------------------------------------------------------------------------"<<endl;
          cout<<"#of| "<<setw(5)<<jettime[1]<<setw(20)<<startime[1]<<setw(20)<<minetime[1]<<setw(20)<<traintime[1]<<endl;
          cout<<"-------------------------------------------------------------------------------"<<endl;
          cout<<"Day| "<<setw(5)<<jettime[0]<<setw(20)<<startime[0]<<setw(20)<<minetime[0]<<setw(20)<<traintime[0]<<endl;
          cout<<"-------------------------------------------------------------------------------"<<endl<<endl<<endl;
          system("PAUSE");
          return 0;
        }
    
    int sector::retrieve(int a)
        {
          switch (a)
           {
             case 1:
               return (credits);
               break;
             case 2:
               return (power);
               break;
             case 3:
               return (metal);
               break;
             case 4:
               return (crew);
               break;
             case 5:
               return (jets);
               break;
             case 6:
               return (starships);
               break;
             case 7:
               return (days);
               break;
             default:
               break;
           }
        }
    sector::~sector()
        {}
    
    int main()
    {
    sector account;
    
    
    do {
        system("cls");
        cout<<"Credits: "<<account.retrieve(1)<<setw(15)<<"Power: "<<account.retrieve(2)<<setw(15)<<"Metal: "<<account.retrieve(3)<<setw(15)<<"Day "<<account.retrieve(7)<<endl;
        cout<<"Crew: "<<account.retrieve(4)<<setw(15)<<"Jets: "<<account.retrieve(5)<<setw(15)<<"Starships: "<<account.retrieve(6)<<endl;
        cout<<"\nWelcome Admiral"<<endl;
        cout<<"------"
            <<"\nOrders"
            <<"\n------"<<endl;
        cout<<"\n[1]Build"
            <<"\n[2]Mine"
            <<"\n[3]Train"
            <<"\n[4]Stats"
            <<"\n[5]Next"
            <<"\n[0]Quit"<<endl;
        cout<<"\nOrder: ";
        cin>>choice;
        cin.ignore();
        switch (choice)
          {
            case 0:
              return 0;
              break;
            case 1:
              account.build();
              break;
            case 2:
              account.mine();
              break;
            case 3:
              account.train();
              break;
            case 4:
              account.stats();
              break;
            case 5:
              account.timer();
              break;
            default:
              break;
          }
       }while (choice != 99);
    return 0;
    }

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>unsigned int jettime[1];
    This only allocates memory for one unsigned int, namely jettime[0]. Note that there is NO jettime[1] allocated with this line.

    >>jettime[0] = 0;
    >>jettime[1] = 0;
    As mentioned above, jettime[1] is past the end of your array. You'll want to declare jettime as
    Code:
    unsigned int jettime[2]; //Allocate memory for 2 elements, jettime[0] and jettime[1].
    The same applies for your other arrays.

    If you were lucky, you computer would have had an illegal operation or something from that(on Windows XP I think it would, though that isn't guaranteed by any means). Too bad you just ended up with the mysterious "after a while my computer randomly dies" error, which is MUCH harder to trace
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Thanks so much man, your a real help. I also got some funny output as well, which was caused by the thing u said. Thanks a billion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Weird memory problems (MS-VC++)
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2006, 08:01 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM
  5. stl::vector problems (memory?)
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2003, 11:21 AM

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