【问题标题】:Segmentation Fault (core dumped) C++分段错误(核心转储)C++
【发布时间】:2013-04-28 23:25:10
【问题描述】:

经过研究,我相信我了解"Segmentation fault" 错误是什么。但是,即使在逐行注释掉代码之后,我似乎也无法找到故障发生的位置来修复它。有没有我忽略的东西导致了这个错误?以下是我运行代码时显示的内容:

准备好玩了吗(是/否)?是的

3C AH 4C 3H 4H 2H 5H 2C 5C 交流

这是你的牌:3C AH 4C 3H 4H

分段错误(核心转储)

下面粘贴的是我所指的代码。被注释掉的部分只是我试图找出错误发生的地方:

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;

vector<string> bigDeck;
vector<string> cardDeck;
vector<string> playerHand;
vector<string> computerhand;
vector<string> shortvec;

const int DEAL_CARDS = 5;


void ResetDeck();
void Shuffle();
void DealACard();
void DealQCard();
void DealBCard();
string CStr(int);
int letsee2;

int main()
{


cout << "Ready to play (y/n)? ";

char yn;
cin >> yn;
if (yn == 'n' || yn != 'y') return 0;

ResetDeck();

srand(time(0));

Shuffle();

for (int f=0; f < 10; f++)
{
        cout << cardDeck[f] << " ";
}


cout << "\n\nHere's your cards: ";
for (int i=0; i < DEAL_CARDS; i++)
{
        DealACard();
        cout << playerHand[i] << " ";
}

cout<<"\n\nHere's the Computer's cards: ";
for (int k=0; k < DEAL_CARDS; k++)
{
        DealBCard();
        cout << computerhand[k] << " ";
}

for (int u=0; u < DEAL_CARDS; u++)
{
        DealQCard();
}

cout<<shortvec.size()<<endl;

cout<<endl<<endl;

//do
//{

for (int woh=0; woh < DEAL_CARDS; woh++)
{
 if ((playerHand[woh][0]=='A') && (computerhand[woh][0]=='A'))
        {
                cout<<"War!"<<endl;


        }
        else if ((playerHand[woh][0]=='A') && (computerhand[woh][0]!='A'))
        {
                cout<<"Player wins"<<endl;
                /*playerHand.push_back(computerhand[woh]);
                computerhand.erase(computerhand.begin()+(woh-1));*/
        }
        else if ((playerHand[woh][0]!='A') && (computerhand[woh][0]=='A'))
        {
                cout<<"Computer Wins"<<endl;
                /*computerhand.push_baci(playerHand[woh]);
                playerHand.erase(playerHand.begin()+(woh-1));*/
        }
        else
        {
                if ((atoi(playerHand[woh].c_str())) > (atoi(computerhand[woh].c_str())))
                {
                        cout<<"Player wins!"<<endl;
                        /*playerHand.push_back(computerhand[woh]);
                        computerhand.erase(computerhand.begin()+(woh-1));*/
                }
                else if ((atoi(playerHand[woh].c_str())) < (atoi(computerhand[woh].c_str())))
                {
                        cout<<"Computer wins!"<<endl;
                        /*computerhand.push_back(playerHand[woh]);
                        playerHand.erase(playerHand.begin()+(woh-1));*/
                }
                else
                {
                        cout<<"War!"<<endl;

                }
        }
/*if (playerHand.size() > computerhand.size())
        shortvec = computerhand;
else
        shortvec = playerHand;

cout<<endl<<endl;
*/
}
/*for (int z=0; z < playerHand.size(); z++)
{
        cout << playerHand[z] << " ";
}

cout<<"\n\nHere's the Computer's cards: ";
for (int y=0; y < computerhand.size(); y++)
{
        cout << computerhand[y] << " ";
}*/

cout<<endl<<endl;
//}
//while(((playerHand.size())!=10) || (computerhand.size())!=10);

return 0;
}
void Shuffle()
{
        random_shuffle(cardDeck.begin(),cardDeck.end());
}

void DealBCard()
{
        computerhand.push_back(cardDeck[0]);
        cardDeck.erase(cardDeck.begin());
}

void DealACard()
{
        playerHand.push_back(cardDeck[0]);
        cardDeck.erase(cardDeck.begin());
}

void DealQCard()
{
        shortvec.push_back(bigDeck[0]);
        bigDeck.erase(bigDeck.begin());
}

string CStr(int n)
{
        stringstream s;
        s << n;
        return s.str();
}

void ResetDeck()
{
        cardDeck.clear();
        playerHand.clear();
        computerhand.clear();

        for (int i=2; i<6; ++i)
        {
                cardDeck.push_back(CStr(i) + "H");
                cardDeck.push_back(CStr(i) + "C");
        }
        cardDeck.push_back("AH");
        cardDeck.push_back("AC");
}

【问题讨论】:

    标签: c++ segmentation-fault


    【解决方案1】:

    您有一个名为bigDeckstd::vector,并且在DealQCard 中,您尝试访问它的第0 个元素,尽管事实上它没有任何元素。您的意思是在bigDeck 中放一些卡片吗?

    【讨论】:

    • 是的,就是这样!非常感谢。我在那里只有那个向量,因为我试图找出一种方法来循环游戏的次数等于最小向量长度(计算机长度与玩家长度)......我相信我是在绕着圈子做的。 ..但是谢谢!这摆脱了分段错误。
    【解决方案2】:

    尝试访问不存在的内存或正在被其他进程使用的内存也会导致分段错误(核心转储)。

    Core dumped 意味着程序状态已被记录,即它在内存和处理器中的资源。

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2017-02-25
      • 2016-07-12
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多