How to fix "segmentation fault (core dumped)" error when accessing a string variable within a struct within a dynamic array

slyce

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 :)

Sam Varshavchik
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.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix 'Segmentation Fault (core dumped)' error

C - Pointer to dynamic array in struct "Segmentation fault (core dumped)"

How to fix Segmentation Fault (core dumped) error when using sprintf for system commands in C++

How to fix Segmentation fault (core dumped) error in C

Getting segmentation fault (core dumped) error when declaring a int variable

segmentation fault (core dumped) error when trying to copy from an array

I got segmentation fault (core dumped) when i use realloc at dynamic array input functions

After upgrading Node, 'Segmentation fault (core dumped)' error shows up. How can I fix it?

How to fix the segmentation fault(core dumped) error on c++ while reading the file?

Segmentation fault (core dumped) when accessing shared process memory

Segmentation fault (core dumped) Error in C program with dynamic arrays

How do I fix "Segmentation fault (core dumped)" in assembly?

How to fix the Segmentation fault (core dumped) in C++?

Segmentation fault (core dumped) copying form array to string

Segmentation fault (core dumped) error while passing string to bool function

Segmentation fault (core dumped) error while deletion of an element in an array

segmentation fault (core dumped) when changing string characters

Segmentation fault (core dumped) when using avx on an array allocated with new[]

Segmentation fault (core dumped) error when creating thread

Segmentation fault (core dumped) error in C when Implementing list ADT

Segmentation fault(core dumped)How I rid this error?

Segmentation fault (core dumped) with array

Error: Segmentation fault (core dumped)

Error: Segmentation fault (core dumped)

Segmentation fault (core dumped) in dynamic memory allocation using a pointer(member) from struct

I get segmentation fault (core dumped) when I tried to run program with struct

Segmentation fault (core dumped) - this error is shown only sometimes and I cannot seem to fix it

Segmentation fault (core dumped) for Struct Pointer Member

segmentation fault(core dumped) in python with swig, but it works when I change the variable name