How do I write to and read the elements of THIS dynamic array of objects?

I have 3 files. 1 main, 1 Player.cpp (Which I fill through asking the user how many instances of it they want, as you can see with the dynamic array) and 1 Player.h


My dynamic array of objects declaration and initialisation is in the main file:

Source.cpp -
1
2
3
4
5
6
Player* players = new Player[Player::getTotalPlayers()];
int main()
{
	players[0].setPlayerName();
	cout << players[0].getPlayerName();
}


Player.h -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Player
{
public:

	//Member functions
	static int getMaximumPlayers();
	static int getTotalPlayers();
	static void setTotalPlayers(int setAmount);
	void setPlayerName();
	std::string getPlayerName();
	void testAccess( );

       // Default Constructor
	Player();
	
	
	int playerScore;
	std::string playerName;


	private:
	//Data members
	static int totalPlayers; // Static data member declaration. For holding total amount of players. Has accessors. 

	static int const MAXIMUM_ALLOWED_PLAYERS = 3; // Ignore this for now.
	
};



Player.cpp -


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

int Player:: totalPlayers = 0;

Player::Player() // Default constructor
{
	

}

void Player::setPlayerName() // Set player name
{


	cin >> playerName;

}


std::string Player::getPlayerName() // Get's player name
{

	return playerName;
}



I keep getting the error(s) :

Exception thrown: write access violation.
_Left was 0xA645.

The program '[5740] 21.exe' has exited with code 0 (0x0)


How would I successfully write to, read and maintain an individual playerName for each Player object declared in the dynamic array? Thank you.




Last edited on
Why should class Player, which essentially holds information about a single player such as his/her name and score, also include information about all players in the game? You need to re-think your design, perhaps a separate class PlayerList which has the list of all Player's etc
Well, in your case totalPlayers is either 0 or uninitialized (depends on the unknown order of initialization of the global/static variables). In both cases accessing the array will probably crash.

It is better not making players global. And totalPlayers requires a value > 0 if you want to access the array.
coder777,

Sorry, I forgot to add all of my source code in main. totalPlayers is not the problem as you will see I have this function in main :
1
2
3
4
5
6
7
8
9
void askPlayerAmount()
{
	int temp_PlayerAmount; // Local variable for player, will be freed from stack(Which is memory for local variable)once the function ends.

	cout << "\nHow many human players would you like in this game:(Enter a positive number) \n";
	cin >> temp_PlayerAmount;
	Player::setTotalPlayers(temp_PlayerAmount);

}


totalPlayers is not the problem, nor is the security of my code. I just want my specific question answered.

How do I write to and read the elements of any dynamic array of objects?


I have read many books and still cannot cypher through my specific problem for the past 3 days. Does anybody know what Im doing wrong?
I may be getting somewhere:

This
1
2
3
4
5
6
Player* players = new Player[Player::getTotalPlayers()];
int main()
{
	players[0].setPlayerName();
	cout << players[0].getPlayerName();
}

Needs to be changed, to this:
1
2
3
4
5
6
int main()
{
Player* players = new Player[Player::getTotalPlayers()];
	players[0].setPlayerName();
	cout << players[0].getPlayerName();
}



as static member Players::totalPlayers is assigned a value AFTER the declaration/initialisation of the dynamic array of Player objects, this meant that my dynamic array was not being assigned a value - so, essentially... they were null elements which would give errors when an attempt was made to acess them, as they didnt exist!.


Can someone clarify?
Last edited on
as static member Players::totalPlayers is assigned a value AFTER the declaration/initialisation of the dynamic array of Player objects
Players::totalPlayers is initialized with the value 0, BEFORE the declaration/initialization of the dynamic array. Unfortunately, an array with a 0 size buys you 0 elements. It's basically a unique memory address that you can neither read from nor write to, so dereferencing via operator[] results in undefined behavior.

With regards to where you placed the initialization of the pointer/array: global initializations do occur before code which executes in main, which may explain your experience if you have code in main affecting the value in question (although there would be no difference with the actual code you've posted here.)

But the answer to this question:
How do I write to and read the elements of any dynamic array of objects?
is the same as any other array of objects. Your real issue seems to be with allocating said array.
Last edited on
So, cire, I am going to keep the global initialisation of the dynamic array. As that is what I wanted in the first place.
And about the
Players::totalPlayers
initialization, I wanted that to have no value in the first place, as the user is supposed to enter a value to be stored in that variable at run time - i.e.: "How many players do you want to play blackjack with" cin >>Players::totalPlayers ?


What I want to do is RESIZE/Allocate/Reallocate that array at run-time with the value Players::totalPlayers and the array being initialised BEFOREHAND, how do I do this? Or cant I do this?

What I want to do is RESIZE/Allocate/Reallocate that array at run-time

If you are allowed to use std::vector, use it instead of poorly reinventing the wheel.

With that said, the naive solution is the following:
1
2
3
4
When you run out of room in the original container:
  Allocate a new, larger block of memory.  
  Copy the contents of the old block of memory to the new block of memory.
  Delete the old block of memory.


The size of any given block of memory is not variable -- this seems to be your misconception.
When you allocate a block of memory with dynamic lifetime and dynamic size, it will not change size again. It must be copied and the old block discarded.
Last edited on
The size of any given block of memory is not variable -- this seems to be your misconception.
When you allocate a block of memory with dynamic lifetime and dynamic size, it will not change size again. It must be copied and the old block discarded.


I will use vectors whenever most appropriate, although, I was required to use both pointers and arrays for my university module -which lead me to delving deep into this conundrum of dynamic arrays.

Thank you, all.
Topic archived. No new replies allowed.