• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Problem passing char*

I'm writing a small game and have run into a small problem. I'm having trouble passing a char*. I have a class called Game which creates two instances of a Player class, like this:

Game::Game() {
      std::cout << "Enter Name for Player 1: ";
      char* name1;
      std::cin >> name1;
      player1 = new Player(name1);
      
      char* name2;
      std::cout << "Enter Name for Player 2: ";
      std::cin >> name2;
      player2 = new Player(name2);
}

Player::Player(char* name) {
      this->name = name;
}

Now, whenever I try and access the name variable like this:

void Player::outputPlayerName() {
      std::cout << "Name: " << name;
}

My program crashes out. Does anyone have any idea why?
0
redneon
Asked:
redneon
  • 2
1 Solution
 
jkrCommented:
>>My program crashes out. Does anyone have any idea why?

Yes, you are reading into some random memory area an uninitialized pointer points to - you should rather use

#define MAX_NAME 255

Game::Game() {
     std::cout << "Enter Name for Player 1: ";
     char name1[MAX_NAME];
     std::cin >> name1;
     player1 = new Player(name1);
     
     char name2[MAX_NAME];
     std::cout << "Enter Name for Player 2: ";
     std::cin >> name2;
     player2 = new Player(name2);
}
0
 
AxterCommented:
I recommend avoiding using a c-style string as an input string to std::cin.
You can use std::string instead.
Also if Player is going to hold on to the address of the string, you need to assign a buffer via new for it to hold on to.

Game::Game() {
     std::string tmp;
     std::cout << "Enter Name for Player 1: ";
     std::cin >> tmp;
     char* name1 = new char[tmp.size()];
     strcpy(name1, tmp.c_str());
     player1 = new Player(name1);
     
     std::cout << "Enter Name for Player 2: ";
     std::cin >> tmp;
     char* name2 = new char[tmp.size()];
     strcpy(name2, tmp.c_str());
     player2 = new Player(name2);
}

Player::Player(char* name) {
     this->name = name;
}

Make sure that your Player destructor calls delete[] on the name, so you don't have a memory leak.
0
 
AxterCommented:
redneon,

If you use jkr's code, your Player object is going to be pointing to a variable that will go out of scope when Game constructor goes out of scope.

I also notice you awarded a B grade to a 50 point question.
If you don't feel the expert gave you an A grade's worth, you should give the expert feedback so-as to give him/her a chance to give you a complete answer.
0
 
redneonAuthor Commented:
Sorry, I graded him because his solution worked. As it happens, I'm going to replace all my char* with std:string anyway =o) As I'm a games programmer I'm loathed to do this, really, but I've got to put my faith in the standard library at some point.

I had realised that the problem came from the pointer going out of scope so I did switch it to store on the heap, rather than the stack. But as I'm using std::string I don't need to worry about it anymore =o)

Thanks for your help, guys.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now