代码在所需的迭代次数之前以“已杀死”消息终止

时间:2014-12-15 21:01:06

标签: c

我的C程序应该运行100000次测试以保证准确性,但是它会在没有引用任何错误的情况下以“Killed”消息终止大约38000,是否有人知道会导致这种情况?该代码的目标是模拟一个名为“Idiots Delight”的纸牌游戏,玩家丢弃的每张牌赢得一美元金额,如果所有牌都被丢弃,则获得累积奖金。目标是确定一个游戏费用,以保证房子每场比赛能获得5美元的利润。这些函数都没有抛出任何错误,但是由于未知原因而退出。我相信它可能是内存泄漏,但我并不积极。

/////From Header File/////
   double Uniform(double a, double b)
/* =========================================================== 
 * Returns a uniformly distributed real number between a and b. 
 * NOTE: use a < b
 * ===========================================================
 */
{ 
  return (a + (b - a) * Random());
}

////来自头文件的函数结束/////

struct NODES {
    int suit; //Clubs(0), Diamonds(1), Hearts(2), Spades(3)
    int rank; //Ace = 1; Jack = 11; Queen = 12; King = 13
    struct NODES *next;
};
typedef struct NODES NODE;

struct CardDeck {
    int clubs[13];
    int diamonds[13];
    int hearts[13];
    int spades[13];
    struct NODES mDeck;
    struct NODES hand;
    NODE * dHead;
    NODE * hHead;
    NODE * refCard;
    NODE * swapA;
    NODE * swapB;
};



struct CardDeck initDeck (struct CardDeck deck){
    deck.refCard = deck.dHead;
    for(int j = 0; j < 4; j++)
        for (int i = 1; i < 14; i++){
            if (deck.refCard == deck.dHead){
                deck.dHead -> suit = j;
                deck.dHead -> rank = i;
                deck.dHead -> next = (NODE *)malloc(sizeof(NODE));
                deck.refCard = deck.dHead -> next;
            }
            else{
                deck.refCard -> suit = j;
                deck.refCard -> rank = i;
                if ((j == 3) && (i == 13)){
                    deck.refCard -> next = NULL;
                }
                else{
                    deck.refCard -> next = (NODE *)malloc(sizeof(NODE));
                    deck.refCard = deck.refCard -> next;
                }
            }
        }
     return deck;
}

struct CardDeck SwapCards(struct CardDeck deck, int src, int dest){  
    deck.swapA = deck.dHead;
    deck.swapB = deck.dHead;
    int tempSuit;
    int tempRank;
    for (int i = 0; i < src; i++){
        deck.swapA = deck.swapA -> next;
    }
    for (int i = 0; i < dest; i++){
        deck.swapB = deck.swapB -> next;
    }
    tempSuit = deck.swapA -> suit;
    tempRank = deck.swapA -> rank;
    deck.swapA -> suit = deck.swapB -> suit;
    deck.swapA -> rank = deck.swapB -> rank;
    deck.swapB -> suit = tempSuit;
    deck.swapB -> rank = tempRank;

    return deck;
}

struct CardDeck shuffleDeck (struct CardDeck deck){
    int src, dest;
    for (int i = 0; i < NCARDS; i++){
        src = i;
        dest = Uniform(0,1);
        deck = SwapCards(deck,src,dest);
    }
    return deck;
}


struct CardDeck initHand(struct CardDeck deck){
    deck.swapA = deck.dHead;
    deck.swapB = deck.dHead;
    for(int i = 0; i < 3; i++){
        deck.swapA = deck.swapA -> next;
    }   
    deck.dHead = deck.swapA -> next;
    deck.hHead = deck.swapB;
    deck.swapA -> next = NULL;
    NODE * first;
    NODE * second;
    NODE * third;
    NODE * fourth;
    first = deck.swapA;
    fourth = deck.hHead;
    deck.hHead = deck.hHead -> next;
    third = deck.hHead;
    deck.hHead = deck.hHead -> next;
    second = deck.hHead;
    deck.hHead = deck.hHead -> next;
    deck.hHead -> next = second;
    second -> next = third;
    third -> next = fourth;
    fourth -> next = NULL;
    deck.refCard = fourth;
    return deck;
}


struct CardDeck Draw(struct CardDeck deck, int inHand){
    NODE * new = (NODE *)malloc(sizeof(NODE));
    new = deck.dHead;
    deck.dHead = deck.dHead -> next;
    new -> next = deck.hHead;
    deck.hHead = new;
    inHand++;
    if(inHand == 1)
        deck.hHead -> next = NULL;
    return deck;
}

int Play(struct CardDeck deck, int fee){
    int cardsRem = NCARDS;
    int inDeck = NCARDS-4;
    int inHand = 4;
    int prize = 0;
    int discards = 0;
    deck = initHand(deck);
    deck.refCard = deck.hHead -> next -> next -> next;
    while ((inDeck || discards) && cardsRem){
        discards = 0;
        while(inHand < 4 && inDeck){
            deck = Draw(deck, inHand);
            inHand++;
            inDeck--;
        }
        if(inHand>3){
        deck.refCard = deck.hHead -> next -> next -> next;
            if(deck.hHead -> rank == deck.refCard -> rank){
                deck.hHead = deck.refCard -> next;
                inHand = inHand - 4;
                cardsRem = cardsRem - 4;
                discards = 1;
            }
            else if(deck.hHead -> suit == deck.refCard -> suit){
                deck.hHead -> next = deck.refCard;
                inHand = inHand - 2;
                cardsRem = cardsRem - 2;
                discards = 1;
            }
            else if(!discards && inDeck){
                deck = Draw(deck,inHand);
                inHand++;
                inDeck--;
            }
        }

    }
    if (cardsRem != 0)
        prize = NCARDS - cardsRem;
    else
        prize = 10000;;
    int profit = fee - prize;
    return profit;
}


void main(){
    struct CardDeck mainDeck;
    int fee = 0;
    float avgProfit = 0;
    float desiredAvgProfit = 5;
    int bestFee = 0;
    for (int i=1;i<14;i++){
        mainDeck.clubs[i-1] = i;
        mainDeck.diamonds[i-1] = i;
        mainDeck.hearts[i-1] = i;
        mainDeck.spades[i-1] = i;
    }
    mainDeck.dHead = (NODE *)malloc(sizeof(NODE));
    mainDeck.dHead -> suit = -1;
    mainDeck.hHead = (NODE *)malloc(sizeof(NODE));
    for(int i = 0; i < 100000; i++){
        fee = 0;
        while(avgProfit < desiredAvgProfit){
            mainDeck = initDeck(mainDeck);
            mainDeck = shuffleDeck(mainDeck);
            printf("%d\n",i);
            avgProfit = (avgProfit + Play(mainDeck, fee))/2;
            fee++;
            }
        avgProfit = 0;
        bestFee = bestFee + fee;
        }
    printf("Ideal Fee: %d\n", bestFee/100000);
}

1 个答案:

答案 0 :(得分:1)

只需快速检查您的代码,我就可以在initDeck中看到您malloc 52张卡,但我在任何地方都看不到对free的任何来电。我希望在Play的某个地方,你应该释放卡片,因为它们已经用完了#34;。所以你是对的,确实存在内存泄漏。