线程1:EXC_BAD_ACCESS(代码= 1地址= 0x0) [英] Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)

查看:285
本文介绍了线程1:EXC_BAD_ACCESS(代码= 1地址= 0x0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的洗牌和交易模拟器.我正在使用一个矢量来表示52张卡片的套,每张卡片都由结构BitCard表示,其元素的存储空间受位域限制.但是,当构造函数尝试访问向量时,xCode会抛出BAD_ACCESS exception: Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0).我进行了一些研究,发现此异常与空指针链接,但似乎无法弄清楚如何解决该异常.我的代码如下:

I am trying to create a simple card shuffling and dealing simulator. I am using a vector to represent a deck of 52 cards and each card is represented by the structure BitCard whose elements' space is memory is limited by bitfields. But when the constructor tries to access the vector, xCode throws a BAD_ACCESS exception: Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0). I've done some research and found that this exception is linked with a null pointer but can't seem to figure out how to fix it. My code is as follows:

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <vector>
#include <iomanip>
using namespace std;
struct BitCard{
    unsigned face:4;
    unsigned color:1;
    unsigned suit:2;
};
class DeckOfCards {
public:
    static const int faces = 13;
    static const int colors = 2;
    static const int numberOfCards = 52;
    DeckOfCards();
    void shuffle();
    void deal();
private:
    vector <BitCard> deck={};
};
DeckOfCards::DeckOfCards(){
    for (int i = 0; i <numberOfCards;++i){
        deck[i].face = i%faces;
        deck[i].suit = i/faces;
        deck[i].color = i/(faces*colors);
    }
}
void DeckOfCards:: shuffle(){
    for (int i = 0; i <numberOfCards;i++){
        int j = rand()%numberOfCards;
        BitCard tmp = deck[i];
        deck[i] = deck[j];
        deck[j] = tmp;
    }
}
void DeckOfCards:: deal(){
    for (int k1 = 0, k2 = k1+numberOfCards/2;k1<numberOfCards/2-1;k1++,k2++)
    {
        cout << "Color:" << setw(3) << deck[k1].color
        << " Card:" << setw(3) << deck[k1].face
        << " Suit:" << setw(3) << deck[k1].suit
        << " Color:" << setw(3) << deck[k2].color
        << " Card:" << setw(3) << deck[k2].face
        << " Card:" << setw(3) << deck[k2].suit;
    }
}



int main(int argc, const char * argv[]) {
    DeckOfCards testDeck;
    testDeck.shuffle();
    testDeck.deal();

    return 0;
}

该异常是在行

deck[i].face = i%faces;

我该如何解决? 预先感谢!

How can I fix this? Thanks in advance!

推荐答案

deck向量的大小始终为0.使用[]索引向量不会会自动调整其大小向量以容纳无效的索引.

The size of your deck vector is always 0. Using [] to index a vector won't automatically resize the vector to accommodate an invalid index.

您可以将其初始化为正确的大小.

You could initialize it to the correct size.

vector <BitCard> deck(numberOfCards);

或在构造函数中调整其大小.

Or resize it in the constructor.

DeckOfCards::DeckOfCards(){
    deck.resize(numberOfCards);

或者您可以在构造函数的循环中使用 push_back 将每张新卡添加到向量的末尾.

Or you can use push_back in the constructor's loop to add each new card to the end of the vector.

DeckOfCards::DeckOfCards(){
    for (int i = 0; i <numberOfCards;++i){
        BitCard card;
        card.face = i%faces;
        card.suit = i/faces;
        card.color = i/(faces*colors);
        deck.push_back( card )
    }
}

这篇关于线程1:EXC_BAD_ACCESS(代码= 1地址= 0x0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆