Thread: Deallocate Memory

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Deallocate Memory

    Hey guys, this is a program for my programming class, but I'm having problems with it. the professor wants us to deallocate memory using a function. I tried this but he said it was wrong and hes pretty useless when you ask for help. so I'm stuck here. the problem is in the last function and I just need to deallocate the pointer declared in main and I'm not sure how it needs to be written. thanks a bunch for any and all help!
    Code:
    #include <cstdlib>
    #include <vector>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cassert>
    #include <iomanip>
    
    
    using namespace std;
    
    
    struct Player
    {
        string first;
        string last;
        int num;
        int min;
        int fga;
        int fgm;
        int tpa;
        int tpm;
        int fta;
        int ftm;
        int off;
        int def;
        int totReb;
        int stl;
        int blk;
        int ast;
        int to;
        int pf;
        int points;
        double tpp;
        double fgp;
        double ftp;
        double pointsPer48;
        double offensiveRating;
        double defensiveRating;
        double mvpRating;
    };
    
    
    bool getPlayerStats(Player*);
    void calcPercentages(Player*);
    void findTopOffensivePlayer(vector<Player*>);
    void findTopDefensivePlayer(vector<Player*>);
    void findMostValuablePlayer(vector<Player*>);
    void outputPlayers(vector<Player*>);
    void deallocatePointers(vector<Player*>);
    
    
    ifstream infile;
    
    
    int main()
    {
        vector <Player*> vStruct;
        Player* ps = new Player;
    
    
        while(getPlayerStats(ps))
        {
            vStruct.push_back(ps);
    
    
            calcPercentages(ps);
            
            ps = new Player;
        }
        outputPlayers(vStruct);
        findTopOffensivePlayer(vStruct);
        findTopDefensivePlayer(vStruct);
        findMostValuablePlayer(vStruct);
        deallocatePointers(vStruct);
    }
    
    
    bool getPlayerStats(Player* ps)
    {
      if(!infile.is_open())
        {
            infile.open("lab3.dat");
        }
    
    
        infile >> ps->num;
    
    
        if(infile.eof())
        {
            infile.close();
            return false;
        } else
        {
            assert(!infile.fail());
        }
    
    
        infile >> ps->first;
        assert(!infile.fail());
    
    
        infile >> ps->last;
        assert(!infile.fail());
    
    
        infile >> ps->min;
        assert(!infile.fail());
    
    
        infile >> ps->fga;
        assert(!infile.fail());
    
    
        infile >> ps->fgm;
        assert(!infile.fail());
    
    
        infile >> ps->tpa;
        assert(!infile.fail());
    
    
        infile >> ps->tpm;
        assert(!infile.fail());
    
    
        infile >> ps->fta;
        assert(!infile.fail());
    
    
        infile >> ps->ftm;
        assert(!infile.fail());
    
    
        infile >> ps->off;
        assert(!infile.fail());
    
    
        infile >> ps->def;
        assert(!infile.fail());
    
    
        infile >> ps->stl;
        assert(!infile.fail());
    
    
        infile >> ps->blk;
        assert(!infile.fail());
    
    
        infile >> ps->ast;
        assert(!infile.fail());
    
    
        infile >> ps->to;
        assert(!infile.fail());
    
    
        infile >> ps->pf;
        assert(!infile.fail());
    
    
        ps->totReb = ps->off + ps->def;
    
    
        ps->points = (2* ps->fgm) + ps->ftm + ps->tpm;
    
    
        return true;
    }
    
    
    void calcPercentages(Player* ps)
    {
        if( ps->points == 0 || ps->min == 0)
        {
            ps->pointsPer48 = 0.0;
        }
        else
        {
            ps->pointsPer48 = (double) ps->points/(ps->min/48);
        }
    
    
        if(ps->fga == 0 || ps->fgm == 0)
        {
            ps->fgp = 0.0;
        }
        else
        {
            ps->fgp = double (ps->fgm) / double (ps->fga);
        }
    
    
        if(ps->tpa == 0 || ps->tpm == 0)
        {
        ps->tpp = 0.0;
        }
        else
        {
            ps-> tpp = double (ps->tpm) / double (ps->tpa);
        }
    
    
        if(ps->fta == 0 || ps->ftm == 0)
        {
        ps->ftp = 0.0;
        }
        else
        {
            ps->ftp = double (ps->ftm) / double (ps->fta);
        }
    }
    
    
    void findTopOffensivePlayer(vector<Player*> vStruct)
    {
        for(int i = 0; i < vStruct.size(); i++)
        {
            vStruct[i]->offensiveRating = (vStruct[i]->pointsPer48 + (vStruct[i]->off * .4) + (vStruct[i]->ast * .4) - (vStruct[i]->to * .4));
        }
    
    
        assert(vStruct.size() > 0);
        int offIndex = 0;
        for (int i = 0; i < vStruct.size(); i++)
        {
            if (vStruct[i]->offensiveRating > vStruct[offIndex]->offensiveRating)
            {
                offIndex = i;
            }
        }
        cout << "Top offensive player: " << vStruct[offIndex]->first << " " << vStruct[offIndex]->last << endl;
    }
    void findTopDefensivePlayer(vector<Player*> vStruct)
    {
        for(int i = 0; i < vStruct.size(); i++)
        {
            vStruct[i]->defensiveRating = (vStruct[i]->def * .4) + (vStruct[i]->blk * .4) + (vStruct[i]->stl * .4) - vStruct[i]->pf;
        }
    
    
        assert(vStruct.size() > 0);
        int defIndex = 0;
        for (int i = 0; i < vStruct.size(); i++)
        {
            if (vStruct[i]->defensiveRating > vStruct[defIndex]->defensiveRating)
            {
                defIndex = i;
            }
        }
        cout << "Top defensive player: " << vStruct[defIndex]->first << " " << vStruct[defIndex]->last << endl;
    }
    void findMostValuablePlayer(vector<Player*> vStruct)
    {
        for(int i = 0; i < vStruct.size(); i++)
        {
            vStruct[i]->mvpRating = (vStruct[i]->offensiveRating * .67) + (vStruct[i]->defensiveRating * .33);
        }
    
    
        assert(vStruct.size() > 0);
        int mvpIndex = 0;
        for (int i = 0; i < vStruct.size(); i++)
        {
            if (vStruct[i]->mvpRating > vStruct[mvpIndex]->mvpRating)
            {
                mvpIndex = i;
            }
        }
        cout << "Most valuable player: " << vStruct[mvpIndex]->first << " " << vStruct[mvpIndex]->last << endl;
    }
    
    
    void outputPlayers(vector<Player*> vStruct)
    {
        int minTotal = 0, fgaTotal = 0, fgmTotal = 0, tpaTotal = 0, tpmTotal = 0, ftaTotal = 0, ftmTotal = 0, rebTotal = 0, stlTotal = 0, blkTotal = 0, astTotal = 0, toTotal = 0, pfTotal = 0, pointsTotal = 0, pp48Total = 0;
    
    
        //cout << vStruct.size() << endl;
        cout << "Player Name\t\tMin  FGA   FGM     PCT  3PA   3PM     PCT   FTA   FTM     PCT  OFF   DEF   TOT  STL  BLK   AST   TO   PF   PTS    P48" << endl;
    
    
        for(int i = 0; i < vStruct.size(); i++)
        {
            cout << vStruct[i]->num << " " << vStruct[i]->first << " " << vStruct[i]->last << "\t\t";
            cout << vStruct[i]->min << " ";
            //shooting
            cout << setw(5) << vStruct[i]->fga << setw(6) << vStruct[i]->fgm << setw(8) << setprecision(2) << vStruct[i]->fgp << setw(5);
            cout << setw(5) << vStruct[i]->tpa << setw(6) << vStruct[i]->tpm << setw(8) << setprecision(2) << vStruct[i]->tpp << setw(5);
            cout << setw(5) << vStruct[i]->fta << setw(6) << vStruct[i]->ftm << setw(8) << setprecision(2) << vStruct[i]->ftp << setw(5);
            //rebounds
            cout << setw(5) << vStruct[i]->off << setw(6) << vStruct[i]->def << setw(5) << vStruct[i]->off + vStruct[i]->def;
            //others
            cout << setw(5) << vStruct[i]->stl << setw(6) << vStruct[i]->blk << setw(5) << vStruct[i]->ast << setw(5) << vStruct[i]->to << setw(5) << vStruct[i]->pf << setw(5) << vStruct[i]->points << " " << setprecision(4) << vStruct[i]->pointsPer48 << endl;
    
    
            minTotal = minTotal + vStruct[i]->min;
            fgaTotal = fgaTotal + vStruct[i]->fga;
            fgmTotal = fgmTotal + vStruct[i]->fgm;
            tpaTotal = tpaTotal + vStruct[i]->tpa;
            tpmTotal = tpmTotal + vStruct[i]->tpm;
            ftaTotal = ftaTotal + vStruct[i]->fta;
            ftmTotal = ftmTotal + vStruct[i]->ftm;
            rebTotal = rebTotal + vStruct[i]->off + vStruct[i]->def;
            stlTotal = stlTotal + vStruct[i]->stl;
            blkTotal = blkTotal + vStruct[i]->blk;
            astTotal = astTotal + vStruct[i]->ast;
            toTotal = toTotal + vStruct[i]->to;
            pfTotal = pfTotal + vStruct[i]->pf;
            pointsTotal = pointsTotal + vStruct[i]->points;
            pp48Total = pp48Total + vStruct[i]->pointsPer48;
        }
            double fgpTotal = fgmTotal/fgaTotal;
            double fppTotal = tpmTotal/tpaTotal;
            double ftpTotal = ftmTotal/ftaTotal;
    
    
            cout << "TOTALS:\t  " << setw(18) << minTotal;
            cout << setw(5) << fgaTotal << setw(6) << fgmTotal << setw(8) << fixed << setprecision(3) << fgpTotal;
            cout << setw(5) << tpaTotal << setw(6) << tpmTotal << setw(8) << fixed << setprecision(3) << fppTotal;
            cout << setw(6) << ftaTotal << setw(6) << ftmTotal << setw(8) << fixed << setprecision(3) << ftpTotal;
            //cout << setw(5) << offTotal << setw(6) << defTotal << setw(6) << rebTotal;
            cout << setw(5) << stlTotal << setw(5) << blkTotal << setw(6) << astTotal << setw(5) << toTotal << setw(5) << pfTotal;
            cout << setw(6) << pointsTotal << setw(7) << endl;
    }
    
    
    void deallocatePointers(vector<Player*> vStruct)
    {
        Player* ps;
        delete ps;
        vStruct.clear();
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Your delete function simply declares a pointer of type ps and then deletes it. Take a look at our Dynamic Memory Allocation - articles. They will show you how to implement this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using free() to deallocate memory
    By Stiletto in forum C Programming
    Replies: 1
    Last Post: 04-24-2011, 07:47 AM
  2. deallocate pointer
    By sangit in forum C++ Programming
    Replies: 16
    Last Post: 03-13-2009, 01:08 PM
  3. Unable to deallocate memory for a tree structure.
    By broli86 in forum C Programming
    Replies: 5
    Last Post: 07-06-2008, 03:30 PM
  4. What can happen if you forget to deallocate memory?
    By avaldi in forum C++ Programming
    Replies: 19
    Last Post: 06-17-2008, 05:33 PM
  5. Deallocate Memory and reuse pointer
    By appleGuy in forum C++ Programming
    Replies: 9
    Last Post: 06-20-2007, 11:07 AM