xcode 线程 1:EXC_BAD_ACCESS(代码 =1 地址 = 0x0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28637022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:41:50  来源:igfitidea点击:

Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)

c++xcodeexc-bad-access

提问by Endi Zhupani

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 BitCardwhose 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:

我正在尝试创建一个简单的洗牌和发牌模拟器。我使用一个向量来表示一副 52 张牌,每张牌都由结构表示,BitCard其元素的空间是受位域限制的内存。但是当构造函数尝试访问向量时,xCode 会抛出一个BAD_ACCESS exception: Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0). 我做了一些研究,发现这个异常与空指针相关联,但似乎无法弄清楚如何修复它。我的代码如下:

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

The exception is generated in line

异常是在线生成的

deck[i].face = i%faces;

How can I fix this? Thanks in advance!

我怎样才能解决这个问题?提前致谢!

回答by Jér?me

You are trying to access an element of your vector deckin the constructor. But when you call the constructor your deckvector is empty

您正在尝试deck在构造函数中访问向量的元素。但是当你调用构造函数时,你的deck向量是空的

vector <BitCard> deck={}; // <- empty deck 

DeckOfCards::DeckOfCards(){
    for (int i = 0; i <numberOfCards;++i){
        deck[i].face = i%faces; // <- At construction time, you are trying to access deck with index i. but Deck is empty!
        //...
    }
}

One solution is to construct your object BitCardand push it to the vector

一种解决方案是构造您的对象BitCard并将其推送到vector

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

回答by Drew Dormann

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

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

You could initialize it to the correct size.

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

vector <BitCard> deck(numberOfCards);

Or resize it in the constructor.

或者在构造函数中调整它的大小。

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

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

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

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 )
    }
}