Program crashing, need good debugger

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 1
Reputation: Treant is an unknown quantity at this point 
Solved Threads: 0
Treant Treant is offline Offline
Newbie Poster

Program crashing, need good debugger

 
0
  #1
Aug 5th, 2006
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

  1. class Player{
  2. protected:
  3. char* PlayerName_;//a string containing the Player's name
  4. Card* CardsDealt_;
  5. //an array representing the cards initially dealt to the Player
  6.  
  7. int Dealt_; //an integer containing the number of cards dealt to the Player
  8.  
  9. void Set(const char*,const Card*,int); //private member function, used to set the object (instantiat), should be called all the constructors
  10.  
  11. void Init(const Player&); //private member funtion called by copy constructor and assignment operator
  12.  
  13. public:
  14. Player(); //defualt constructor, PlayerName = "unknown" and the Player initially holds no cards by calling the Set private member function
  15.  
  16. 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
  17.  
  18. 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
  19.  
  20. Player(const Player&); //copy constructor
  21.  
  22. ~Player(); //destructor
  23.  
  24. 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
  25.  
  26. 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
  27.  
  28. int numcards() const; //query that returns the number of cards held
  29.  
  30. 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
  31.  
  32. const char* name() const; //function returns the name of the Player
  33.  
  34. void Print() const; //function prints the card held by the Player. Cards must be printed in a SORTED manner from low to high
  35.  
  36. Player& operator=(const Player&); //assignment operator
This is the end of the .h file

This is the start of the .cpp file

  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5. #include "a5.h"
  6.  
  7. /*************
  8. *PLAYER CLASS*
  9. **************/
  10.  
  11. //private member function, used to set the object (instantiat),
  12. //should be called all the constructors
  13. void Player::Set(const char* PlayerName,const Card* CardsDealt,int Dealt){//start function Set
  14.  
  15. //cards dealt to the player
  16. if(Dealt_ < 0)
  17. Dealt_ = 0; //set to 0 if Dealt_ is less than 0, safe empty state
  18. else
  19. Dealt_ = Dealt; //set to Dealt_ if more than 0
  20.  
  21. //Players name
  22. if(PlayerName_ != NULL){//start if
  23. //allocate freestore memory for Player Name
  24. PlayerName_ = new char[strlen(PlayerName) + 1];
  25. strcpy(PlayerName_,PlayerName);
  26. PlayerName_[strlen(PlayerName)] = '\0';
  27. }//end if
  28.  
  29. //safe empty state
  30. else{ //start else
  31. PlayerName_ = new char[1];
  32. strcpy(PlayerName_,"");
  33. }//end else
  34.  
  35. //Array with cards initially dealt to the Player
  36. if(CardsDealt_ != NULL){
  37. CardsDealt_ = new Card[Dealt]; //allocate freestore memory for Cards Dealt
  38. //start of for loop
  39. for(int i=0; i<Dealt; i++){
  40. CardsDealt_[i] = CardsDealt[i];
  41. }//end of for loop
  42. }
  43. else
  44. CardsDealt_ = NULL; //setting the pointer to NULL, ie no cards
  45. }//end function Set
  46.  
  47.  
  48. //defualt constructor, PlayerName = "unknown" and
  49. //the Player initially holds no cards by calling
  50. //the Set private member function
  51. Player::Player(){
  52.  
  53. //calling Set function
  54. Set("Unknown",0,0);
  55. }
  56.  
  57. //3 argument constructor, takes in a char value,
  58. //a Card value and an int and calls the Set private
  59. //member function
  60. Player::Player(const char* PlayerName,const Card* CardsDealt,int Dealt){
  61.  
  62. //calling Set function
  63. Set(PlayerName,CardsDealt,Dealt);
  64. }
  65.  
  66. //2 argument constructor, takes in a Card value and
  67. //an int, in this case the value of PlayerName = "Unknown",
  68. //calls the Set private member function
  69. Player::Player(const Card* CardsDealt,int Dealt){
  70.  
  71. //calling Set function
  72. Set("Unknown",CardsDealt,Dealt);
  73. }
  74.  
  75. //copy constructor for Player class
  76. Player::Player(const Player& source){
  77.  
  78. //calls Init
  79. Init(source);
  80. }
  81.  
  82. //assignment operator for Player
  83. Player& Player:[IMG]http://www.3dbuzz.com/vbforum/images/smilies/redface.gif[/IMG]perator=(const Player& source){
  84.  
  85. //start of if statement
  86. if(this != &source){
  87. //delete PlayerName
  88. if (PlayerName_)
  89. delete [] PlayerName_;
  90. //delete CardsDealt
  91. if (CardsDealt_)
  92. delete [] CardsDealt_;
  93.  
  94. //calls Init
  95. Init(source);
  96. }//end of if
  97. return *this;
  98. }
  99.  
  100. //called by copy constructor and assignment operator
  101. void Player::Init(const Player& source){
  102.  
  103. //Player Name
  104. PlayerName_ = new char[strlen(source.PlayerName_) + 1];
  105. strcpy(PlayerName_,source.PlayerName_);
  106.  
  107. //Cards Dealt
  108. CardsDealt_ = new Card[source.Dealt_];
  109.  
  110. //Dealt
  111. Dealt_ = source.Dealt_;
  112.  
  113. //start of for loop
  114. for(int i=0; i<source.Dealt_; i++){
  115. CardsDealt_[i] = source.CardsDealt_[i];
  116. }//end of for loop
  117. }
  118. //function recieves an array const Card cardsdealt[]
  119. //(represents the cards dealt to the Player) and an
  120. //int numcards (number of cards dealt). This function
  121. //replaces any cards currently held by the player with
  122. //the cards in the cardsdealt array
  123. void Player:[IMG]http://www.3dbuzz.com/vbforum/images/smilies/biggrin.gif[/IMG]eal(const Card cardsdealt[],int numcards){
  124.  
  125. //deallocate Cards Dealt
  126. if (CardsDealt_)
  127. delete [] CardsDealt_;
  128. //reallocate Cards Dealt
  129. CardsDealt_ = new Card[numcards];
  130. //start of for loop
  131. for(int i=0; i<numcards; i++){
  132. CardsDealt_[i] = cardsdealt[i];
  133. }//end of for loop
  134. }
  135.  
  136. //function recieves a reference to a Card, then finds
  137. //lowest Card that the Player holds and then places the
  138. //lowest Card's value into the Card refered to by c
  139. void Player::GetLowest(Card& c) const{
  140.  
  141. //call Sort() funtion from a4.h to sort the cards from lowest to highest, putting the lowest card in CardsDealt_[0]
  142. Sort(CardsDealt_,Dealt_);
  143.  
  144. //puts the lowest card from the CardsDealt_ array into c
  145. c = CardsDealt_[0];
  146.  
  147. }
  148.  
  149. //query that returns the number of cards held
  150. int Player::numcards() const{
  151.  
  152. return Dealt_;
  153. }
  154.  
  155. //function copies held by Player into cardsheld array.
  156. //If maxsize is less than the number of cards held by
  157. //the Player, only copy maxsize cards. Function returns
  158. //number of cards copied into the cardsheld array
  159. int Player::GetCards(Card cardsheld[],int maxsize) const{
  160.  
  161. //integer containing the number of times the loop will go
  162. int NumberOfTimes;
  163.  
  164. if(maxsize < Dealt_)
  165. NumberOfTimes = maxsize;
  166. else if(maxsize > Dealt_)
  167. NumberOfTimes = Dealt_;
  168.  
  169. //delete cardsheld[]
  170. if(cardsheld)
  171. delete [] cardsheld;
  172. //reallocate memory for cardsheld[]
  173. cardsheld = new Card[NumberOfTimes];
  174. //start of for loop
  175. for(int i=0; i<NumberOfTimes ; i++){
  176. cardsheld[i] = CardsDealt_[i]; //copying the cards from the Player into cardsheld
  177. }//end of for loop
  178.  
  179. return NumberOfTimes;
  180. }
  181.  
  182. //query returns the name of the Player
  183. const char* Player::name() const{
  184.  
  185. return PlayerName_;
  186. }
  187.  
  188. //function prints the card held by the Player.
  189. //Cards must be printed in a SORTED manner from low to high
  190. void Player::Print() const{
  191.  
  192. //sort cards from low to high
  193. Sort(CardsDealt_,Dealt_);
  194.  
  195. //start of for loop
  196. for(int i=0; i<Dealt_; i++){
  197. cout << CardsDealt_[i].Suit() << ' '; //prints out the the suit values aka the letters held by player
  198. }//end of for loop
  199.  
  200. cout << endl; //next line
  201.  
  202. //start of for loop
  203. for(int i=0; i<Dealt_; i++){
  204. cout << CardsDealt_[i].Face() << ' '; //prints out the face values aka the numbers held by player
  205. }//end of for loop
  206.  
  207. cout << endl; //next line
  208. }
  209.  
  210. //deallocate freestore memory, destructor
  211. Player::~Player(){
  212.  
  213. //delete PlayerName
  214. if (PlayerName_)
  215. delete [] PlayerName_;
  216. //delete CardsDealt
  217. if (CardsDealt_)
  218. delete [] CardsDealt_;
  219. }


This is the end of the .cpp file



Thanks
Treant
Last edited by Dave Sinkula; Aug 5th, 2006 at 10:37 pm. Reason: Added [code][/code] tags -- learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Program crashing, need good debugger

 
1
  #2
Aug 6th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Program crashing, need good debugger

 
1
  #3
Aug 6th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC