Problem deleting dynamically allocated char array

This is a discussion on Problem deleting dynamically allocated char array within the C++ Programming forums, part of the General Programming Boards category; As the title states, I'm having problems deleting a dynamic character array. I run the program and it runs fine ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    26

    Problem deleting dynamically allocated char array

    As the title states, I'm having problems deleting a dynamic character array. I run the program and it runs fine until I delete the character array that holds the player's name.

    It gives me an error like this.

    Code:
    Debug Assertion Failed!
    
    <directory to dbgdel.cpp> line 52
    
    Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
    Here is all the code that I believe to be related to the error
    Code:
    //in file Main.cpp
    int main()
    {
    	CGame* Game = new CGame;
    	Game->CreatePlayer();
    	delete Game;
    	Game = NULL;
    
    	return 0;
    }
    Code:
    //in file Game.cpp
    CGame::~_CGame(void)
    {
    	CGame::CleanUp();
    }
    
    void CGame::CreatePlayer(void)
    {
    	char name[50];
    	cout << "What is your name?\n";
    	cout << ">";
    	cin.getline(name, 50);
    	m_Player->SetName(name);
    	cout << "Your name is " << m_Player->GetName() << ".\n";
    	cin.get();
    }
    
    void CGame::CleanUp(void)
    {
    	delete m_Player;
    	m_Player = NULL;
    }
    Code:
    //in file Player.cpp
    CPlayer::_CPlayer(void)
    {
    	m_Name = new char[50];
    }
    CPlayer::~_CPlayer(void)
    {
    	CPlayer::CleanUp();
    }
    
    char* CPlayer::GetName(void)
    {
    	return m_Name;
    }
    void CPlayer::SetName(char* name)
    {
    	m_Name = name;
    }
    
    void CPlayer::CleanUp(void)
    {
    	delete[] m_Name;
    	m_Name = NULL;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is m_Name just a char *? If so, it is pointing to the local memory in the CreatePlayer() function, which means that (1) any attempt to use it outside that function will fail horribly and (2) it is not dynamically allocated memory and so should not be freed.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    Ah I had forgot to make the temporary name variable dynamically allocated and then send it over. Thank you very much.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Why are you not using std::string?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    I was going to but had problems using getline with string. That and I don't have much experience messing with c-style strings, so I decided to use them instead to become more familiar with them.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Quote Originally Posted by Rubiks14 View Post
    I was going to but had problems using getline with string.
    Really? What sort of problems were you having with them?

    Quote Originally Posted by Rubiks14 View Post
    That and I don't have much experience messing with c-style strings, so I decided to use them instead to become more familiar with them.
    Bad idea. First of all, you should thoroughly practice using them with a 'test' program (and this really goes for anything you're not familiar with) before ever using them in your regular code. Otherwise, you're just setting yourself up for disaster. Second, the whole beauty of C++ is that you can leave all of those implemantation details to a higher level class (which is both better at managing memory and manipulation than some naive approach).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. Problem deleting dynamic array
    By pliang in forum C++ Programming
    Replies: 7
    Last Post: 04-11-2005, 04:07 PM
  4. Replies: 5
    Last Post: 12-05-2002, 01:27 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21