简体   繁体   English

分段错误(核心转储)C ++

[英]Segmentation Fault (core dumped) C++

After researching I do believe I understand what the "Segmentation fault" error is. 经过研究,我相信我理解"Segmentation fault"错误是什么。 However, even after commenting out the code line by line, I can not seem to find where the fault is occurring in order to fix it. 然而,即使在逐行注释掉代码之后,我似乎无法找到故障发生的位置以便修复它。 Is there something I am overlooking that is causing this error? 我有什么东西可以忽略导致这个错误吗? Below is what shows up when I run the code: 以下是我运行代码时显示的内容:

Ready to play (y/n)? 准备玩了(是/否)? y ÿ

3C AH 4C 3H 4H 2H 5H 2C 5C AC 3C AH 4C 3H 4H 2H 5H 2C 5C AC

Here's your cards: 3C AH 4C 3H 4H 这是你的卡片:3C AH 4C 3H 4H

Segmentation fault (core dumped) 分段故障(核心转储)

Pasted below is the code that I am referring to. 下面粘贴的是我所指的代码。 The commented out parts were just me trying to find where the error was occurring: 注释掉的部分只是我试图找出错误发生的位置:

#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");
}

You have a std::vector called bigDeck and in DealQCard you attempt to access its 0th element, despite the fact it has no elements. 你有一个名为bigDeckstd::vector ,在DealQCard你尝试访问它的第0个元素,尽管它没有元素。 Did you mean to put some cards in bigDeck ? 你的意思是在bigDeck放一些卡吗?

Trying to access non-existent memory or memory which is being used by other process also causes the Segmentation Fault (core dumped). 尝试访问其他进程正在使用的不存在的内存或内存也会导致分段错误(核心转储)。

Core dumped means that the program state has been recorded, ie, its resources that were in memory and processor. 核心转储意味着已经记录了程序状态,即其内存和处理器中的资源。

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

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