I am programming a simple game and I am keeping track of the users stats for each game they play in a session while the program is running. To do this, I made a struct called "game" which holds the statistics of the game played(game #, who won, who lost,# of turns, and if they chose to resign). I then made a dynamic array of these structs, which allocates one more section of memory for a new struct each time the number of games played goes up by one. Then all the statistics are outputted by running a for-loop which goes through and outputs the stats of each game played in that session. When outputting the int variable which holds the number of the game there is no problem, but once it reaches the string variable that holds the name of the winner I get a "segmentation fault (core dumped)" error.
I understand the error message means I am trying to access memory that I have no access too, so I made sure my dynamic array's capacity was larger than the index so I wasn't going outside of it's memory location, and even added extra capacity after each game but the error still persists.
I believe this part is whats causing the error when I try to access the "winner" string, just not sure why:
for(int i = 0; i <= index; i++){
cout<<"Game "<<games[i].num<<"\t|\t"<<games[i].winner<<end;
}
This is how I made my dynamic array:
int index = 0;
int capacity = 1;
game *games;
games = (game *)malloc(sizeof(game)*capacity);
This is how I expand the array for a new game struct each time a new game is played. Index goes up by one after each game, this code should make sure that capacity remains the same or larger than the index.
if (index>=capacity) {
capacity+=1;
games = (game *) realloc(games, sizeof(game)*capacity);
}
This is the struct definition
struct game
{
int num;
string winner;
};
Sorry in advance for any bad formatting, I am a noob at C++ and I am also new to this website. Be nice please :)
games = (game *)malloc(sizeof(game)*capacity);
games = (game *) realloc(games, sizeof(game)*capacity);
This game
contains a C++ class called std::string
. This is a class with a constructor and a destructor.
malloc()
and realloc
are C library functions, that know absolutely nothing, whatsoever, about any C++ class, or how to call its constructors or destructors.
You cannot use malloc
, realloc
, or free
with C++ classes. Not std::string
, or any other non-POD C++ class.
Fortunately, C++ has a very useful class called std::vector
that will correctly manage a "dynamic array", correctly growing and shrinking, and allocating memory for its contents, and correctly constructing and destroying everything that needs to be constructed or destroyed.
Your code reinvents a wheel that has already been invented, called std::vector
. It does everything the shown code manually does, with regards to size, capacity, etc... You're trying to duplicate std::vector
's logic, without any apparent benefit.
Completely get rid of all your code that uses malloc
, realloc
, and free
, and replace it with std::vector
. You will find more information on how to use std::vector
in your C++ book.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments