删除 [] 期间出现分段错误

Segmentation Fault during delete []

我正在构建游戏,每次创建玩家时我都需要将玩家存储在动态数组中。我构建了一小段代码来尝试它,当我尝试 delete 和 table 来插入第三个播放器时,我得到了 segmentation fault。我只是不明白为什么会这样:header 文件是:

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class Player

{

    private:
        string PlayerName;
        int PlayerScore;
    public:
        Player();       
        Player(string name, int s);     
        ~Player() {};       
        string getPlayerName()  {return PlayerName;}
        int getPlayerScore() {return PlayerScore;}
        void setPlayerName(string name){PlayerName = name;}
        void setPlayerScore(int score){PlayerScore = score;}
};

class Game
{

    private:
        Player NewPlayer;
        int NPlayers;
        Player* PlayerList;
        //vector <Player> PlayerList2;
    
        
    public:
        Game();
        ~Game();
        void setNewPlayer(string, int); 
        void resizePlayerList();
        void PrintList();   
};

class 文件:

#include <iostream>
#include <string>
#include <cstring>
#include <memory>
#include "I.h"

using namespace std;


    
Player::Player()
{
    PlayerName = "";
    PlayerScore = 0;
}

Player::Player(string name, int s)
{
    PlayerName = name;
    PlayerScore = s;
}       


Game::Game() 
{
    NPlayers = 0;
    PlayerList = NULL;
};
Game::~Game() {};

void Game::setNewPlayer(string str, int scr)
{
    NewPlayer = Player(str, scr);   
    resizePlayerList(); 
    PrintList();        
}


void Game::resizePlayerList() {
   
    if(NewPlayer.getPlayerName() != "No Name")
    {
            int newSize = NPlayers +1;
            
            Player* newArr = NULL;
            newArr = new Player[newSize];           
            memcpy( newArr, PlayerList, NPlayers * sizeof(Player) );            
            NPlayers = newSize;         
            delete [] PlayerList;           
            PlayerList = newArr;            
            PlayerList[NPlayers-1] = NewPlayer;
    } 
   
}


void Game::PrintList()
{
    Player player;
    //cout << NPlayers << endl;
    for(int i= 0; i < NPlayers; i++)
    {
        player = PlayerList[i];     
        cout << player.getPlayerName() << " " <<  player.getPlayerScore() << endl;  
    }

}

main:

#include <iostream>
#include <string>
#include "I.h"

using namespace std;

int main()
{
    Game NewGame;
    NewGame.setNewPlayer("Peter",20);
    NewGame.setNewPlayer("Someone Someone",30);
    NewGame.setNewPlayer("Someone else",40);
    return 0;
}

问题出在这里:

memcpy(newArr, PlayerList, NPlayers * sizeof(Player));

不能以这种方式复制类,除非它们Trivially Copyable (originally said POD, but as Yksisarvinen points out in the comments, memcpy没有那么严格)。您可以通过使用循环来复制数据来解决此问题:

for (int i = 0; i < NPlayers; ++i) {
    newArr[i] = std::move(PlayerList[i]);
}

不过,更好的选择是使用 std::vector<Player>,然后整个调整大小功能可以简化为:

void Game::resizePlayerList() {
    if (NewPlayer.getPlayerName() != "No Name") {
        PlayerList.push_back(std::move(NewPlayer));
    }
}