Arrays 在内存中动态分配内存
我用c语言创建了四连胜游戏。现在我想动态分配电路板的内存。这是一个二维数组,我希望它存储在那里以供使用。我将其声明为全局双指针,并在下面代码中的初始化方法中声明它。我是c语言的新手,我不完全确定如何处理我得到的分段错误(内核转储)错误Arrays 在内存中动态分配内存,arrays,c,memory-management,Arrays,C,Memory Management,我用c语言创建了四连胜游戏。现在我想动态分配电路板的内存。这是一个二维数组,我希望它存储在那里以供使用。我将其声明为全局双指针,并在下面代码中的初始化方法中声明它。我是c语言的新手,我不完全确定如何处理我得到的分段错误(内核转储)错误 #include <string.h> #include <stdlib.h> #include <ctype.h> #define MAX 50 //size of board #define rows 6 #define
#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;
}
#包括
#包括
#包括
#定义最大值50
//板的尺寸
#定义第6行
#定义第7列
//玩家姓名
字符播放器1[100];
字符播放器2[100];
//用于跟踪播放回合的指针
char*playersTurn;
//计数器来跟踪我们在每个列上的位置
int a=行-1;
int b=行-1;
int c=行-1;
int d=行-1;
int e=行-1;
int f=行-1;
int g=行-1;
//电路板阵列
//int板[行][列];
int**板;
//铺板阵元
void boardlayArray(){
printf(“二维数组元素:\n”);
对于(inti=0;i如何使用heap??这是程序员动态内存分配的目标,下面是一个简单的2d数组分配代码:
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 */
}
intmain()
{
int r=3,c=4,i,j,计数;
int*arr[r];
//为数组分配内存
对于(i=0;i变量a
到g
看起来像是一个伪装的数组。使用这些显式名称进行编码将是一场地狱(或者,至少是重复的和容易出错的)。但是,仔细检查后,您似乎没有使用这些变量。在一个函数中(checkFour()
),您将其中4个变量隐藏在同名参数后面。您不会检查内存分配是否成功(或程序是否成功读取播放器名称)。不这样做会带来麻烦-虽然分配失败的可能性很小。但是一旦出现崩溃问题,您应该确保内存分配没有失败;这是遇到麻烦的更快方法之一。哦,我刚刚在UpdateWorld()中找到了对a
的引用。g
)
UpdateWorld
非常复杂。数组不使用变量a、b、c、d、e、f、g,而是更简单、更不容易出错,它将UpdateWorld
中的行数大致除以4到5。在这个程序中,我有时没有处理错误malloc()失败时抛出,您可以这样做。2.您也可以不使用free()和malloc(),而使用realloc()。我不是专业人士,只是分享我的想法和代码,您可以根据需要修改2d数组大小。