繁体   English   中英

二维数组中的分段错误——获取一个奇怪的地址

[英]Segmentation Fault in 2D Array--Getting a weird address

这是我的指针以及二维数组。

private:
        Die *d1,
            *d2;
        int rounds[20][2], // 20 rounds and 2 players
            numRounds = 0;

然后我在构造函数中使用 null 指针解除分配

 d1 = nullptr;
 d2 = nullptr;

一旦用户选择了他们选择的骰子,就将它们设置为新的。

if (dieChoice == 1)
        {
            d1 = new Die(sides);
        }

        else if (dieChoice == 2)
        {
            d2 = new LoadedDie(sides);
        }

我的分段错误来自这里

for (int i = 0; i < numRounds; i++)
    {
        rounds[i][0] = d1 -> roll(); //Sometimes here
        rounds[i][1] = d2 -> roll(); //Sometimes here
    }

当我通过 Onlinegdb 输入并打印回合地址时,我得到了这个

$1 = {{0, 0}, {6, 0}, {6304008, 0}, {4198032, 0}, {6304008, 0}, {-147844055,                              
    32767}, {4, 0}, {-5216, 32767}, {6303168, 0}, {4202634, 0}, {-148024840,                              
    32767}, {65535, 1}, {-5200, 32767}, {4202656, 0}, {5, 0}, {4202749, 0}, {                             
    124, 0}, {0, 0}, {4202672, 0}, {4198336, 0}}  

我理解为什么基于上述内容会发生分段错误,但我对如何修复它还不是很清楚。

这是我的两个文件的代码,以防万一有人仍然感到困惑

//Game.hpp

#include <iostream>
#include "loadedDie.hpp"
#include "die.hpp"
#ifndef GAME_HPP
#define GAME_HPP


class Game
{
    private:
        Die *d1,
            *d2;
        int rounds[20][2],
            numRounds = 0;

    public:
       Game();
       void mainMenu();
       void letsPlay();
       void results();

};

#endif
//Game.cpp
#include "game.hpp"
#include <cstdlib>
#include <ctime>
#include <iomanip>

Game::Game()
{
    d1 = nullptr;
    d2 = nullptr ;
}

void Game::mainMenu()
{   
    int dieChoice = 0, sides = 0;
    srand(static_cast<unsigned int>(time(nullptr)));

    std::cout <<"\nWelcome to War with Dice\n" ;

    std::cout << "How many rounds do you want? (Max is 20)\n" ;
    std::cin >> numRounds ;

    while ( !std::cin >> numRounds || numRounds < 1 || numRounds > 20)
        {
                std::cout << "Invalid: Please enter a number between 1 and 20.\n" <<std::endl;
                std::cin.clear();
                std::cin.ignore(256, '\n');
                std::cin >> numRounds;
                std::cin.ignore(256, '\n');
         }

        std::cout << "\nPlayer 1: \n" ;
        std::cout << "What type of die would you like? \n";
        std::cout << "1. Regular Die\n" ;
        std::cout << "2. Loaded Die\n" ;
        std::cin >> dieChoice ;


        while (!std::cin >> dieChoice || dieChoice < 1 || dieChoice > 2 )
            {
                std::cout << "Invalid: Please ONLY enter the numbers listed.\n" <<std::endl;
                std::cin.clear(); 
                std::cin.ignore(256, '\n');
                std::cin >> dieChoice;

                if (dieChoice == 1 || dieChoice == 2)
                {
                    break; 
                }

                std::cin.ignore(256, '\n');
            }


        std::cout << "\nHow many sides would you like: 6, 10, or 14?\n" ;
        std::cin >> sides ;

        switch (sides)
        {
            case 6:
            {
               break;
            }
            case 10:
            {
               break;
            }
            case 14:
            {
               break; 
            }
            default:
            {
              std::cout << "\nInvalid input: Please enter ONLY the numbers listed.\n" ; 
              std::cin.clear(); 
              std::cin.ignore(256, '\n');
              std::cin >> sides;
              std::cin.ignore(256, '\n');
            }
        }

        if (dieChoice == 1)
        {
            d1 = new Die(sides);
        }

        else if (dieChoice == 2)
        {
            d2 = new LoadedDie(sides);
        }


        std::cout << "\nPlayer 2: \n" ;
        std::cout << "What type of die would you like? \n";
        std::cout << "1. Regular Die\n" ;
        std::cout << "2. Loaded Die\n" ;
        std::cin >> dieChoice ;

        while (dieChoice < 1 || dieChoice > 2 )
                {
                      std::cout << "Invalid: Please ONLY enter the numbers listed.\n" <<std::endl;
                      std::cin.ignore(256, '\n');
                      std::cin.clear(); 
                      std::cin >> dieChoice;

                      if (dieChoice == 1 || dieChoice == 2)
                        {
                            break; 
                        }

                       std::cin.ignore(256, '\n'); 
                }

        std::cout << "\nHow many sides would you like: 6, 10, or 14?\n" ;
        std::cin >> sides ;

        switch (sides)
        {
            case 6:
            {
               break;
            }
            case 10:
            {
               break;
            }
            case 14:
            {
               break; 
            }
            default:
            {
              std::cout << "\nInvalid input: Please enter ONLY the numbers listed.\n" ; 
              std::cin.clear(); 
              std::cin.ignore(256, '\n');
              std::cin >> sides;
              std::cin.ignore(256, '\n');
            }
        }

        if (dieChoice == 1)
        {
            d1 = new Die(sides);
        }

        else if (dieChoice == 2)
        {
            d2 = new LoadedDie(sides);
        }

}

void Game::letsPlay()
{ 
    for (int i = 0; i < numRounds; i++)
    {
        rounds[i][0] = d1 -> roll();
        rounds[i][1] = d2 -> roll();
    }

}

void Game::results()
{
    int scorePlayer1 = 0,
        scorePlayer2 = 0;

    system ("clear") ;

    std::cout << "Player 1             Player 2\n" ; 
    for (int i = 0; i < numRounds; i++)
    {
        std::cout<<rounds[i][0]<<"\t \t\t"<<rounds[i][1]<<std::endl;

        if(rounds[i][0]>rounds[i][1])
        {
            scorePlayer1++;
        }
        else if(rounds[i][0]<rounds[i][1])
        {
            scorePlayer2++;
        }
    }

    std::cout<< "\nPlayer 1 won "<< scorePlayer1<<" times."<<std::endl;
    std::cout<< "Player 2 won "<< scorePlayer2<<" times."<<std::endl;

    if(scorePlayer1 > scorePlayer2)
    {
        std::cout<< "\nPlayer 1 wins!\n"; 
    }

    else if (scorePlayer1 < scorePlayer2)
    {
        std::cout<<"\nPlayer 2 wins!\n";
    }

    else
    {
        std::cout<<"\nIts a draw!\n";
    }


    std::cout << "\nHere were your choices:\n" ;
    std::cout << "\nPlayer 1's choices are...\n" ;
    std::cout << "Die: " <<  d1 -> getdieType() << "\n";
    std::cout << "Sides: " << d1 -> getN()<< "\n";

    std::cout << "\nPlayer 2's choices are...\n";
    std::cout << "Die: " << d2 -> getdieType() << "\n";
    std::cout << "Sides: " << d2 -> getN() << "\n";

    std::cout << "\nNumber of Rounds: " << numRounds << "\n"; 

}

谢谢

根据选择, d1d2中的一些可能保持为nullptr

那个部分

        if (dieChoice == 1)
        {
            d1 = new Die(sides);
        }

        else if (dieChoice == 2)
        {
            d2 = new LoadedDie(sides);
        }

应该

        if (dieChoice == 1)
        {
            d1 = new Die(sides);
        }

        else if (dieChoice == 2)
        {
            d1 = new LoadedDie(sides);
        }

(将d2更改为d1 )玩家 1 和

        if (dieChoice == 1)
        {
            d2 = new Die(sides);
        }

        else if (dieChoice == 2)
        {
            d2 = new LoadedDie(sides);
        }

(将d1更改为d2 )玩家 2。

此外,即使使用此代码,当letsPlay()时未调用mainMenu() () 时, d1d2可能仍为nullptr ,因此最好在调用其成员函数之前添加检查它们是否为nullptr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM