Hey everyone

Yeah so I have this program that I'm coding and when I compile it crashes (I don't know where the source of the error is). So I've parsed through it many times to no avail. I'm using the Borland C++ compiler and the debugger that comes with it. But this particular debugger doesn't seem to be too good. Anyone know of any that I could use?

If someone could have a look at my code quickly (much <3 if you can) or point me to a good debugger that could help me find this memory leak I will be greatfull.

Here is my code, in which the problem is...

This is the start of the .h file

class Player{
protected:
   char* PlayerName_;//a string containing the Player's name
   Card* CardsDealt_;
//an array representing the cards       initially dealt to the Player

 int Dealt_; //an integer containing the number of cards dealt to the Player

 void Set(const char*,const Card*,int); //private member function, used to set the object (instantiat), should be called all the constructors

   void Init(const Player&);                    //private member funtion called by copy constructor and assignment operator

public:
 Player(); //defualt constructor, PlayerName = "unknown" and the Player initially holds no cards by calling the Set private member function

 Player(const char*,const Card*,int); //3 argument constructor, takes in a char value, a Card value and an int and calls the Set private member function

 Player(const Card*,int); //2 argument constructor, takes in a Card value and an int, in this case the value of PlayerName = "Unknown", calls the Set private member function

   Player(const Player&);                        //copy constructor

   ~Player();                            //destructor

 void Deal(const Card cardsdealt[],int numcards); //function recieves an array const Card cardsdealt[] (represents the cards dealt to the Player) and an int numcards (number of cards dealt). This function replaces any cards currently held by the player with the cards in the cardsdealt array

 void GetLowest(Card& c) const; //function recieves a reference to a Card, then finds lowest Card that the Player holds and then places the lowest Card's value into the Card refered to by c

   int numcards() const;                                           //query that returns the number of cards held

 int GetCards(Card cardsheld[],int maxsize) const; //function copies held by Player into cardsheld array. If maxsize is less than the number of cards held by the Player, only copy maxsize cards. Function returns number of cards copied into the cardsheld array

   const char* name() const;                                       //function returns the name of the Player

 void Print() const; //function prints the card held by the Player. Cards must be printed in a SORTED manner from low to high

   Player& operator=(const Player&);                //assignment operator

This is the end of the .h file

This is the start of the .cpp file

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "a5.h"

/*************
*PLAYER CLASS*
**************/

//private member function, used to set the object (instantiat),
//should be called all the constructors
void Player::Set(const char* PlayerName,const Card* CardsDealt,int Dealt){//start function Set

   //cards dealt to the player
   if(Dealt_ < 0)
       Dealt_ = 0;        //set to 0 if Dealt_ is less than 0, safe empty state
   else
       Dealt_ = Dealt;    //set to Dealt_ if more than 0

   //Players name
   if(PlayerName_ != NULL){//start if
       //allocate freestore memory for Player Name
       PlayerName_ = new char[strlen(PlayerName) + 1];
       strcpy(PlayerName_,PlayerName);
       PlayerName_[strlen(PlayerName)] = '\0';
   }//end if

   //safe empty state
   else{    //start else
       PlayerName_ = new char[1];
       strcpy(PlayerName_,"");
   }//end else

   //Array with cards initially dealt to the Player
   if(CardsDealt_ != NULL){
       CardsDealt_ = new Card[Dealt];    //allocate freestore memory for Cards Dealt
       //start of for loop
       for(int i=0; i<Dealt; i++){
           CardsDealt_[i] = CardsDealt[i];
       }//end of for loop
   }
   else
       CardsDealt_ = NULL;                //setting the pointer to NULL, ie no cards
}//end function Set


//defualt constructor, PlayerName = "unknown" and
//the Player initially holds no cards by calling
//the Set private member function
Player::Player(){

   //calling Set function
   Set("Unknown",0,0);
}

//3 argument constructor, takes in a char value,
//a Card value and an int and calls the Set private
//member function
Player::Player(const char* PlayerName,const Card* CardsDealt,int Dealt){

   //calling Set function
   Set(PlayerName,CardsDealt,Dealt);
}

//2 argument constructor, takes in a Card value and
//an int, in this case the value of PlayerName = "Unknown",
//calls the Set private member function
Player::Player(const Card* CardsDealt,int Dealt){

   //calling Set function
   Set("Unknown",CardsDealt,Dealt);
}

//copy constructor for Player class
Player::Player(const Player& source){

   //calls Init
   Init(source);
}

//assignment operator for Player
Player& Player:[IMG]http://www.3dbuzz.com/vbforum/images/smilies/redface.gif[/IMG]perator=(const Player& source){

   //start of if statement
   if(this != &source){
       //delete PlayerName
       if (PlayerName_)
           delete [] PlayerName_;
       //delete CardsDealt
       if (CardsDealt_)
           delete [] CardsDealt_;

       //calls Init
       Init(source);
   }//end of if
   return *this;
}

//called by copy constructor and assignment operator
void Player::Init(const Player& source){

   //Player Name
   PlayerName_ = new char[strlen(source.PlayerName_) + 1];
   strcpy(PlayerName_,source.PlayerName_);

   //Cards Dealt
   CardsDealt_ = new Card[source.Dealt_];

   //Dealt
   Dealt_ = source.Dealt_;

   //start of for loop
   for(int i=0; i<source.Dealt_; i++){
       CardsDealt_[i] = source.CardsDealt_[i];
   }//end of for loop
}
//function recieves an array const Card cardsdealt[]
//(represents the cards dealt to the Player) and an
//int numcards (number of cards dealt). This function
//replaces any cards currently held by the player with
//the cards in the cardsdealt array
void Player:[IMG]http://www.3dbuzz.com/vbforum/images/smilies/biggrin.gif[/IMG]eal(const Card cardsdealt[],int numcards){

   //deallocate Cards Dealt
   if (CardsDealt_)
       delete [] CardsDealt_;
   //reallocate Cards Dealt
   CardsDealt_ = new Card[numcards];
   //start of for loop
   for(int i=0; i<numcards; i++){
       CardsDealt_[i] = cardsdealt[i];
   }//end of for loop
}

//function recieves a reference to a Card, then finds
//lowest Card that the Player holds and then places the
//lowest Card's value into the Card refered to by c
void Player::GetLowest(Card& c) const{

   //call Sort() funtion from a4.h to sort the cards from lowest to highest, putting the lowest card in CardsDealt_[0]
   Sort(CardsDealt_,Dealt_);

   //puts the lowest card from the CardsDealt_ array into c
   c = CardsDealt_[0];

}

//query that returns the number of cards held
int Player::numcards() const{

   return Dealt_;
}

//function copies held by Player into cardsheld array.
//If maxsize is less than the number of cards held by
//the Player, only copy  maxsize cards. Function returns
//number of cards copied into the cardsheld array
int Player::GetCards(Card cardsheld[],int maxsize) const{

   //integer containing the number of times the loop will go
   int NumberOfTimes;

   if(maxsize < Dealt_)
       NumberOfTimes = maxsize;
   else if(maxsize > Dealt_)
       NumberOfTimes = Dealt_;

   //delete cardsheld[]
   if(cardsheld)
       delete [] cardsheld;
   //reallocate memory for cardsheld[]
   cardsheld =  new Card[NumberOfTimes];
   //start of for loop
   for(int i=0; i<NumberOfTimes ; i++){
       cardsheld[i] = CardsDealt_[i];        //copying the cards from the Player into cardsheld
   }//end of for loop

   return NumberOfTimes;
}

//query returns the name of the Player
const char* Player::name() const{

   return PlayerName_;
}

//function prints the card held by the Player.
//Cards must be printed in a SORTED manner from low to high
void Player::Print() const{

   //sort cards from low to high
   Sort(CardsDealt_,Dealt_);

   //start of for loop
   for(int i=0; i<Dealt_; i++){
       cout << CardsDealt_[i].Suit() << ' ';        //prints out the the suit values aka the letters held by player
   }//end of for loop

   cout << endl;    //next line

   //start of for loop
   for(int i=0; i<Dealt_; i++){
       cout << CardsDealt_[i].Face() << ' ';        //prints out the face values aka the numbers held by player
   }//end of for loop

   cout << endl;    //next line
}

//deallocate freestore memory, destructor
Player::~Player(){

   //delete PlayerName
   if (PlayerName_)
       delete [] PlayerName_;
   //delete CardsDealt
   if (CardsDealt_)
       delete [] CardsDealt_;
}

This is the end of the .cpp file

Thanks
Treant

Pare back the program to a single method within the class and compile that, then add methods back one at a time making sure each one compiles as you go. When it no longer compiles, you'll have a good start at where to look. Alternatively, pare back one method at a time until it does compile. The last function removed before it compiles is likely (but not gauranteed) to be problematic.

1. Your class it at least missing it's closing ;

2. By failing to use code tags, certain class member functions have been replaced by smilies.

3. Why on earth are you still using char* to store strings. This is C++, and you already include <string>, so why not just use it for every string?

Likewise using <vector> to store all the cards rather than rolling your own array is probably safer as well.

4. PlayerName_[strlen(PlayerName)] = '\0';
This is a waste of time - arr[strlen(arr)] is always \0, so in the end nothings changed.

This article has been dead for over six months. Start a new discussion instead.