codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Program crashing, need good debugger  Treant at 01:06 on Sunday, August 06, 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

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_ = CardsDealt;
}//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::operator=(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_ = source.CardsDealt_;
}//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::Deal(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_ = cardsdealt;
}//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 = CardsDealt_; //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_.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_.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

  Re: Program crashing, need good debugger  Gord T at 01:07 on Tuesday, November 21, 2006
 

I will look at the code but for now you can find a good c++ prog at (Microsoft visual c++ 2005 express)

http://msdn.microsoft.com/vstudio/express/visualc/

You will have to update some files to run Win32 Windows
and you will need to download (free) SDK and MSDN files
from the same Microsoft site.

The express version is the same as the pro version except no code optimizing and no .res file editing although you can edit res files in text mode as per usual. Otherwise...
-Gord.

<Added>

First off, in your player class, where is CARD declared?
re: Card* CardsDealt_; ???

<Added>

At the end of your class declaration:
Player& operator=(const Player&); //assignment operator
you should of had.. };
I assume that was an error in pasting your source into this document.
....

<Added>

Anyways, I can't continue debugging until you give me a definition for CARD.
-Gord.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: Security - Code verify
•  Job @ EarlySail
•  Job @ EarlySail (perl)
•  IPC problem
•  Re: import contacts of msn/yahoo
•  Cookies and Threads C++
•  right justify a background in a table?
•  Help with Loop (C++/MFC)
•  Help with Loop (C++/MFC)


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007