簡體   English   中英

動態分配內存

[英]Dynamically Allocating Memory In

我在 c 中創建了四連勝游戲。 現在我想動態分配板子的內存。 它是一個二維數組,我希望它存儲在那里以供使用。 我將它聲明為全局雙指針,並在下面代碼的初始化方法中聲明它。 我是 c 的新手,我不完全確定如何處理我得到的分段錯誤(核心轉儲)錯誤。

#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 50

//size of board
#define rows 6
#define column 7



//player's name 
char player1[100];
char player2[100];

//pointer to keep track of playes turn
char *playersTurn;

//counter to keep track of where we are on each column  
int a = rows - 1;
int b = rows - 1;
int c = rows - 1;
int d = rows - 1;
int e = rows - 1;
int f = rows - 1;
int g = rows - 1;

//array of the board
//int board[rows][column];
int **board;

//boardlaying array elements
void boardlayArray(){
   printf("Two Dimensional array elements:\n");
   for(int i=0; i<rows; i++) {
      for(int j=0;j<column;j++) {
         printf("%d ", board[i][j]);
         if(j==6){
            printf("\n");
         }
      }
   }
}


//recostruct board to empty places 
void teardown(){
   /*Counter variables for the loop*/
   int i, j;
   for(int i=0; i<rows; i++) {
      for(int j=0;j<column;j++) {
      board[i][j]=0;
      }
   }
   boardlayArray();
}

//checks if the four in a row are the same
int checkFour(int a, int b, int c, int d){
    return a == b && b == c && c == d;
}


//check for the horizontal win
void checkHorizontal(int player){
    for(int i=0; i < rows; i++){
        for(int j=0; j < column - 3; j++){
            if ((board[i][j] != 0) && (board[i][j+1] != 0) && (board[i][j+2]!= 0) && (board[i][j+3] != 0)){
                if (checkFour(board[i][j],board[i][j+1],board[i][j+2],board[i][j+3]) == 1){
                printf("Game Over\n");
                exit(0);
                }
            }
        }
    }
}

//check for vertical win
void checkVertical(int player){
    for(int j=0; j < column; j++){
        for(int i=rows - 1; i > rows - 3; i--){
            if ((board[i][j] != 0) && (board[i-1][j] != 0) && (board[i-2][j]!= 0) && (board[i-3][j] != 0)){
                if (checkFour(board[i][j],board[i-1][j],board[i-2][j],board[i-3][j]) == 1){
                printf("Game Over\n");
                exit(0);
                }
            }
        }
    }
}

//check for diagonal win
void checkDiagonal (int player){
     // ascendingDiagonalCheck
    for (int i=3; i<column; i++){
        for (int j=0; j<rows-3; j++){
            if ((board[i][j] != 0) && (board[i-1][j+1] != 0) && (board[i-2][j+2]!= 0) && (board[i-3][j+3] != 0)){
                if (checkFour(board[i][j],board[i-1][j+1],board[i-2][j+2],board[i-3][j+3]) == 1){
                    printf("Game Over\n");
                    exit(0);
                }
            }
        }
    }
    // descendingDiagonalCheck
    for (int i=3; i<column; i++){
        for (int j=3; j<rows; j++){
            if ((board[i][j] != 0) && (board[i-1][j-1] != 0) && (board[i-2][j-2]!= 0) && (board[i-3][j-3] != 0)){
                if (checkFour(board[i][j],board[i-1][j-1],board[i-2][j-2],board[i-3][j-3]) == 1){
                    printf("Game Over\n");
                    exit(0);
                }
            }
        }
    }
}
 

//places the players puck into the correct place and gives it a corresponding value
//Also checks and correspondingly updtaes the turn to same player if row is full
int updateWorld(char w, int playerNumber){
    switch(w)
    {
        case 'A':
        if(a == -1){
            printf("This row is full\n");
            return 0;
        }
        board[a][0] = playerNumber;
        a--;
        break;
        
        case 'B' : 
        if(b == -1){
            printf("This row is full\n");
            return 0;
        }
        board[b][1] = playerNumber;
        b--;
        break;
        
        case 'C' : 
        if(c == -1){
            printf("This row is full\n");
            return 0;
        }
        board[c][2] = playerNumber;
        c--;
        break;
        
        case 'D' : 
        if(d == -1){
            printf("This row is full\n");
            return 0;
        }
        board[d][3] = playerNumber;
        d--;
        break;
        
        case 'E' : 
        if(e == -1){
            printf("This row is full\n");
            return 0;
        }
        board[e][4] = playerNumber;
        e--;
        break;
        
        case 'F' :
        if(a == -1){
            printf("This row is full\n");
            return 0;
        }
        board[f][5] = playerNumber;
        f--;        
        break;
        
        case 'G' :
        if(g == -1){
            printf("This row is full\n");
            return 0;
        }
        board[g][6] = playerNumber;
        g--;        
        break;
        
        case ' ' : printf("Nothing was entered\n");
        break;
        
        case 'Q' : printf("%s has quit game\n", playersTurn);
        teardown();
        exit(0);
        break;
        
        default: printf("Enter a wrong option\n");
    }
    
    boardlayArray();
    checkHorizontal(playerNumber);
    checkVertical(playerNumber);
    checkDiagonal(playerNumber);
    return 1;
    
}
//get player's names and dynamicaaly allocate memory for board
void initialization(){
    printf("Enter the name of player 1:\n");
    fgets(player1,MAX, stdin);
    
    printf("Enter the name of player 2:\n");
    fgets(player2,MAX, stdin);
    
    board = (int **)malloc(rows * sizeof(int *)); 
    for (int i=0; i<rows; i++) 
         board[i] = (int *)malloc(column * sizeof(int)); 
}

//get where player wants to play 
char acceptInput(){
    printf("Enter where you want to put the disc A-G. Enter Q if you want to quit.\n");
    char w;
    scanf("%[^\n]",&w);
    
    return w;
}


int main(){
    initialization();

    char w;
    //runs for the size of the board
    for(int i = 1; i <= (rows * column); i++){
        w = acceptInput();
        w = toupper(w);
        if(i%2 == 0){
            playersTurn = (char*) player2;
            if(updateWorld(w,2) == 0){
                playersTurn = (char*) player1;
                printf("%s won the game",playersTurn);
                exit(0);
            }
        }
        
        else{
            playersTurn = (char*) player1;
            if(updateWorld(w,1) == 0){
                playersTurn = (char*) player2;
                printf("%s won the game",playersTurn);
                exit(0);
            }
        }
        
        while(getchar() != '\n'); 
        
        
    }
    
    return 0;
    
}







如何使用堆? 這是動態內存分配的程序員的goto,這是一個簡單的二維數組分配代碼:

int main()
{
    int r = 3, c = 4, i, j, count;

    int *arr[r];
    //allocating memory for an array
    for (i=0; i<r; i++)
         arr[i] = (int *)malloc(c * sizeof(int));
    /*code to debug */

    //free memory before allocating again 
    for(i=0; i<r; i++)
        free(arr[i]);

    //reallocating memory for an array
    for(i=0; i<r+2; i++)
        arr[i]  = (int *)malloc(c * sizeof(int));
    return 0;
    /* code to work on reallocated array */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM