简体   繁体   English

动态分配内存

[英]Dynamically Allocating Memory In

I created the four in a row game in c.我在 c 中创建了四连胜游戏。 Now I want to dynamically allocate the memory of the board.现在我想动态分配板子的内存。 It is a two-dimensional array and I want it stored there for use.它是一个二维数组,我希望它存储在那里以供使用。 I declare it as a global double-pointer and declare it in my initialization method in the code below.我将它声明为全局双指针,并在下面代码的初始化方法中声明它。 I am new to c and I'm not entirely sure how to go about handling the segmentation fault(core dumped) error that I get.我是 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;
    
}







How About using the heap??如何使用堆? That's the goto of a programmer for dynamic memory allocation , here is a simple 2d array allocation code :这是动态内存分配的程序员的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