分段错误:11 - 但建立成功

时间:2013-03-17 16:11:04

标签: c#

我正在创建一个BlackJack游戏,到目前为止我已经创建了一个Card Class 和甲板课,我给我的甲板课一个打印功能,看看是否已创建 卡片并正确地洗牌。但是当我运行程序时,我得到了这个 错误,分段错误:11,但构建成功。

主要类

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace BlackJackGameX
    {
        public class MainClass
        {
            public static void Main (string[] args)
            {

                Deck CardDeck = new Deck();

                CardDeck.Print ();

            }
        }
    }

卡类

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public enum Suit {Spades, Hearts, Clubs, Diamonds}
    public enum FaceValue {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}

    public class Card
    {
        public Suit CardSuit;
        public FaceValue CardValue;
        public int iValue;

        public Card (Suit cardSuit, FaceValue cardValue, int ivalue)
        {
            CardSuit = cardSuit;
            CardValue = cardValue;
            iValue =ivalue;
        }

        public Card ()
        {

        }
    }
}

甲板上课

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public class Deck
    {
        Random rNumber = new Random();

        List<Card> Cards;

        public int iValue1 = 11;
        public int iValue2 = 2;
        public int iValue3 = 3;
        public int iValue4 = 4;
        public int iValue5 = 5;
        public int iValue6 = 6;
        public int iValue7 = 7;
        public int iValue8 = 8;
        public int iValue9 = 9;
        public int iValue10 = 10;
        public int iValue11 = 10;
        public int iValue12 = 10;
        public int iValue13 = 10;

        public Deck()
        {
            Cards = NewDeck();
            Shuffle();
        }

        public void Shuffle()
        {

            for (int i = 0; i <= 51; ++i) 
            {
                int c = rNumber.Next (1, 53);

                Cards.Add(Cards[c]);
            }
        }

        private List<Card> NewDeck()
        {
            var AllSuits = new Suit[]
            {
                Suit.Spades,
                Suit.Hearts,
                Suit.Clubs,
                Suit.Diamonds
            };

            var AllFaces = new FaceValue[]
            {
                FaceValue.Ace,
                FaceValue.Two,
                FaceValue.Three,
                FaceValue.Four,
                FaceValue.Five,
                FaceValue.Six,
                FaceValue.Seven,
                FaceValue.Eight,
                FaceValue.Nine,
                FaceValue.Ten,
                FaceValue.Jack,
                FaceValue.Queen,
                FaceValue.King
            };

            var AllValues = new int[]
            {
                iValue1,
                iValue2,
                iValue3,
                iValue4,
                iValue5,
                iValue6,
                iValue7,
                iValue8,
                iValue9,
                iValue10,
                iValue11,
                iValue12,
                iValue13
            };

            for (int i = 0; i <= 3; i++) 
            {
                for (int j = 0; j <= 12; j++) 
                {
                    Card newCard = new Card(AllSuits[i], AllFaces[j], AllValues[j]);
                    NewDeck().Add(newCard);
                }
            }

            return NewDeck();
        }

        public void Print()
        {
            Console.WriteLine(Cards[1].CardSuit);
            Console.WriteLine(Cards[1].CardValue);
            Console.WriteLine(Cards[1].iValue);
            Console.ReadLine();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您的问题出在方法Deck的课程private List<Card> NewDeck()中。当你编写NewDeck().Add(newCard);时,你一次又一次地调用函数递归,直到发生堆栈溢出。解决方案是为您的套牌定义一个新的本地List<Card>变量,您要在其中添加卡片并返回此变量而不是NewDeck()

在您的两个for循环之前添加List<Card> myDeck = new List<Card>();并将行NewDeck().Add(newCard);更改为myDeck.Add(newCard);,将return NewDeck();更改为return myDeck;

答案 1 :(得分:1)

当构建成功时,这意味着编译器能够从代码生成目标二进制文件。但是,这并不意味着您的应用程序没有错误。这只意味着编译成功了。

Segmentation fault是内存错误,发生在运行时。所以你必须首先成功构建。这是预期的。

您需要做的是调试代码以确定发生此内存错误的原因。

您是否取消引用NULL指针?您是否正在执行超出阵列限制的操作?你是否溢出缓冲区(并覆盖指针)?这些是seg的常见原因。故障。错误。

所以,我想说你的问题更多是关于调试(如何调试分段错误错误)。为此,我们需要知道错误发生的原因。