C - 变量“ch1”周围的堆栈已损坏

如何解决C - 变量“ch1”周围的堆栈已损坏

我试图在 c 中制作国际象棋游戏。这是基本的。这是给我一个朋友的。但是当我尝试玩游戏时出现此错误:错误是“变量 'ch1' 周围的堆栈已损坏”。我认为这是关于重载这个 ch1 数组,它是 char 数组(player1() 函数中的数组)。但我不知道我在哪里重载了这个变量。你们能帮我解决这个问题吗?

#define _CRT_SECURE_NO_WARNINGS

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int pwstatus[8] = { 0,0 };
int pbstatus[8] = { 0,0 };

struct piece {
    int color; //0 if white,1 if black,2 if empty
    int type;//1:pawn,2:rook,3:knight,4:bishop,5:queen,6:king,0 if empty
};

char* pieceToChar(piece piece) {
    char *ch = (char*)malloc(5);
    if (piece.color == 0)
        ch[0] = 'W';
    if (piece.color == 1)
        ch[0] = 'B';
    if (piece.color == 2) {
        ch[0] = '0';
        ch[1] = ' ';
        ch[2] = ' ';
        ch[3] = ' ';
        ch[4] = '\0';
        return ch;
    }
    if (piece.type == 1)
        ch[1] = 'P';
    if (piece.type == 2)
        ch[1] = 'R';
    if (piece.type == 3)
        ch[1] = 'K';
    if (piece.type == 4)
        ch[1] = 'B';
    if (piece.type == 5)
        ch[1] = 'Q';
    if (piece.type == 6)
        ch[1] = 'G';
    ch[2] = ' ';
    ch[3] = ' ';
    ch[4] = '\0';
    return ch;
}

void assignChar(char* char1,char* char2,int n) {
    for (int i = 0; i < n; i++) {
        char1[i] = char2[i];
    }
}

void charToInt(char* char1,int* int1) {
    if (char1[0] == 'A') {
        int1[0] = 0;
    }
    if (char1[0] == 'B') {
        int1[0] = 1;
    }
    if (char1[0] == 'C') {
        int1[0] = 2;
    }
    if (char1[0] == 'D') {
        int1[0] = 3;
    }
    if (char1[0] == 'E') {
        int1[0] = 4;
    }
    if (char1[0] == 'F') {
        int1[0] = 5;
    }
    if (char1[0] == 'G') {
        int1[0] = 6;
    }
    if (char1[0] == 'H') {
        int1[0] = 7;
    }
    if (char1[1] == '1') {
        int1[1] = 0;
    }
    if (char1[1] == '2') {
        int1[1] = 1;
    }
    if (char1[1] == '3') {
        int1[1] = 2;
    }
    if (char1[1] == '4') {
        int1[1] = 3;
    }
    if (char1[1] == '5') {
        int1[1] = 4;
    }
    if (char1[1] == '6') {
        int1[1] = 5;
    }
    if (char1[1] == '7') {
        int1[1] = 6;
    }
    if (char1[1] == '8') {
        int1[1] = 7;
    }
}

void intToChar(int* int1,char* char1) {
    if (int1[0] == 0) {
        char1[0] = 'A';
    }
    if (int1[0] == 1) {
        char1[0] = 'B';
    }
    if (int1[0] == 2) {
        char1[0] = 'C';
    }
    if (int1[0] == 3) {
        char1[0] = 'D';
    }
    if (int1[0] == 4) {
        char1[0] = 'E';
    }
    if (int1[0] == 5) {
        char1[0] = 'F';
    }
    if (int1[0] == 6) {
        char1[0] = 'G';
    }
    if (int1[0] == 7) {
        char1[0] = 'H';
    }
    if (int1[1] == 0) {
        char1[1] = '1';
    }
    if (int1[1] == 1) {
        char1[1] = '2';
    }
    if (int1[1] == 2) {
        char1[1] = '3';
    }
    if (int1[1] == 3) {
        char1[1] = '4';
    }
    if (int1[1] == 4) {
        char1[1] = '5';
    }
    if (int1[1] == 5) {
        char1[1] = '6';
    }
    if (int1[1] == 6) {
        char1[1] = '7';
    }
    if (int1[1] == 7) {
        char1[1] = '8';
    }
}

char intToCharDif(int integer) {
    char ch = '0';
    if (integer == 0) {
        ch = 'A';
    }
    if (integer == 1) {
        ch = 'B';
    }
    if (integer == 2) {
        ch = 'C';
    }
    if (integer == 3) {
        ch = 'D';
    }
    if (integer == 4) {
        ch = 'E';
    }
    if (integer == 5) {
        ch = 'F';
    }
    if (integer == 6) {
        ch = 'G';
    }
    if (integer == 7) {
        ch = 'H';
    }
    return ch;
}


struct Board{
    piece board[8][8];

    Board() {
        piece piece;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (i < 2) 
                    piece.color = 0;
                if (i > 5)
                    piece.color = 1;
                if (j == 0)
                    piece.type = 2;
                if (j == 1)
                    piece.type = 3;
                if (j == 2)
                    piece.type = 4;
                if (j == 3)
                    piece.type = 5;
                if (j == 4)
                    piece.type = 6;
                if (j == 5)
                    piece.type = 4;
                if (j == 6)
                    piece.type = 3;
                if (j == 7)
                    piece.type = 2;
                if (i > 1 && i < 6) {
                    piece.color = 2;
                    piece.type = 0;
                }
                if (i == 1 || i == 6) {
                    piece.type = 1;
                }
                board[i][j] = piece;
            }
        }
    }

    void print() {
        printf("Chess Board: \n");
        char ch[5] = "0000";
        for (int i = 8; i >= 0; i--) {
            if (i == 8)
                printf("8  ");
            else if (i == 7)
                printf("7  ");
            else if (i == 6)
                printf("6  ");
            else if (i == 5)
                printf("5  ");
            else if (i == 4)
                printf("4  ");
            else if (i == 3)
                printf("3  ");
            else if (i == 2)
                printf("2  ");
            else if (i == 1)
                printf("1  ");
            else
                printf("   ");
            for (int j = 0; j < 8; j++) {
                if (i == 0)
                    break;
                assignChar(&ch[0],pieceToChar(board[i - 1][j]),5);
                printf("%s",ch);
            }
            printf("\n");
        }
        printf("   A   B   C   D   E   F   G   H\n");
    }
    int pawnMove(int r1,int c1) {
        char ch;
        int mCount = 0;
        printf("Press: ");
        if (board[r1][c1].color == 0) {
            if (pwstatus[c1] == 1)
            {
                if (board[r1 + 1][c1].color == 2) {
                    ch = intToCharDif(c1);
                    printf("%c%d,",ch,r1 + 1);
                    mCount++;
                }

                if (board[r1 + 2][c1].color == 2) {
                    ch = intToCharDif(c1);
                    printf("%c%d,r1 + 2);
                    mCount++;
                }

            }
            else
            {
                if (board[r1 + 1][c1].color == 2) {
                    ch = intToCharDif(c1);
                    printf("%c%d,r1 + 1);
                    mCount++;
                }

                if (board[r1 + 1][c1 + 1].color == 1) {
                    ch = intToCharDif(c1 + 1);
                    printf("%c%d,r1 + 1);
                    mCount++;
                }

                if (board[r1 + 1][c1 - 1].color == 1) {
                    ch = intToCharDif(c1 - 1);
                    printf("%c%d,r1 + 1);
                    mCount++;
                }
            }
        }
        else {
            if (pbstatus[c1] == 1)
            {
                if (board[r1 - 1][c1].color == 2) {
                    ch = intToCharDif(c1);
                    printf("%c%d,r1 - 1);
                    mCount++;
                }
                   
                if (board[r1 - 2][c1].color == 2) {
                    ch = intToCharDif(c1);
                    printf("%c%d,r1 - 2);
                    mCount++;
                }
                    
            }
            else
            {
                if (board[r1 - 1][c1].color == 2) {
                    ch = intToCharDif(c1);
                    printf("%c%d,r1 - 1);
                    mCount++;
                }
                
                if (board[r1 - 1][c1 - 1].color == 0) {
                    ch = intToCharDif(c1 - 1);
                    printf("%c%d,r1 - 1);
                    mCount++;
                }

                if (board[r1 - 1][c1 + 1].color == 0) {
                    ch = intToCharDif(c1 + 1);
                    printf("%c%d,r1 - 1);
                    mCount++;
                }   
            }
        }
        if (mCount == 0)
        {
            printf(" There is no available move!\nTry different piece!\n");
        }
        return mCount;
    }
    int rookMove(int r1,int c1) {
        int n;
        int mCount = 0;
        printf("Press: ");
        int color = board[r1][c1].color;
        n = c1;

        char ch;

        while (board[r1][n - 1].color == 2)
        {
            if (n == 0) { break; }
            ch = intToCharDif(n - 1);
            printf("%c%d,r1);
            n--;
            mCount++;
        }

        n = c1;

        while (board[r1][n + 1].color == 2 && (n + 1) <= 7)
        {
            ch = intToCharDif(n + 1);
            printf("%c%d,r1);
            ++n;
            mCount++;
        }


        n = r1;

        while (board[n - 1][c1].color == 2 && n > -1)
        {
            ch = intToCharDif(c1);
            printf("%c%d,n - 1);
            --n;
            mCount++;
        }

        n = r1;

        while ((board[n + 1][c1].color == 2) && ((n) <= 7))
        {
            ch = intToCharDif(c1);
            printf("%c%d,n + 1);
            ++n;
            mCount++;
        }

        if (mCount == 0)
        {
            printf(" There is no available move!\nTry different piece!\n");
        }
        return mCount;
    }
    int knightMove(int r1,int c1) {
        printf("Press: ");
        int color = board[r1][c1].color;
        char ch;
        int mCount = 0;
        if (board[r1 + 2][c1 + 1].color == 2) {
            ch = intToCharDif(c1 + 1);
            printf("%c%d,r1 + 2);
            mCount++;
        }

        if (board[r1 + 2][c1 - 1].color != color) {
            ch = intToCharDif(c1 - 1);
            if ((c1 - 1) > -1) printf("%c%d,r1 + 2);
            mCount++;
        }

        if (board[r1 + 1][c1 + 2].color != color) {
            ch = intToCharDif(c1 + 2);
            if ((c1 + 2) != 8) printf("%c%d,r1 + 1);
            mCount++;
        }
        if (board[r1 - 1][c1 + 2].color != color) {
            ch = intToCharDif(c1 + 2);
            printf("%c%d,r1 - 1);
            mCount++;
        }

        if (board[r1 - 2][c1 - 1].color != color){
            ch = intToCharDif(c1 - 1);
            if ((c1 - 1) != -1)
                printf("%c%d,r1 - 2);
            mCount++;
        }

        if (board[r1 - 2][c1 + 1].color != color) {
            ch = intToCharDif(c1 + 1);
            printf("%c%d,r1 - 2);
            mCount++;
        }
            
        if (board[r1 + 1][c1 - 2].color != color) {
            ch = intToCharDif(c1 - 2);
            printf("%c%d,r1 + 1);
            mCount++;
        }
            

        if (board[r1 - 1][c1 - 2].color != color){
            ch = intToCharDif(c1 - 2);
            if ((c1 - 2) != -1)
                printf("%c%d,r1 - 1);
            mCount++;
        }
        if (mCount == 0)
        {
            printf(" There is no available move!\nTry different piece!\n");
        }
        return mCount;
    }
    int bishopMove(int r1,int c1) {
        int a,b;
        printf("Press: ");
        int color = board[r1][c1].color;
        char ch;
        int mCount = 0;

        a = 1,b = 1;

        while (board[r1 - a][c1 + b].color != color)
        {
            ch = intToCharDif(c1 + b);
            if ((r1 - a) == -1 || (c1 + b) == 8) break;
            printf("%c%d,r1 - a);
            a++;
            b++;
            mCount++;
        }


        a = 1,b = 1;

        while (board[r1 + a][c1 - b].color != color)
        {
            ch = intToCharDif(c1 - b);
            if ((r1 + a) == 8 || (c1 - b) == -1) break;
            printf("%c%d,r1 + a);
            a++;
            b++;
            mCount++;
        }

        a = 1,b = 1;


        while (board[r1 + a][c1 + b].color != color)
        {
            ch = intToCharDif(c1 + b);
            if ((r1 + a) == 8 || (c1 + b) == 8) break;
            printf("%c%d,r1 + a);
            a++;
            b++;
            mCount++;
        }

        a = 1;
        b = 1;

        while (board[r1 - a][c1 - b].color != color)
        {
            ch = intToCharDif(c1 - b);
            if ((r1 - a) == -1 || (c1 - b) == -1) break;
            printf("%c%d,r1 - a);
            a++;
            b++;
            mCount++;
        }
        if (mCount == 0)
        {
            printf(" There is no available move!\nTry different piece!\n");
        }
        return mCount;
    }
    int queenMove(int r1,int c1) {
        char ch;
        int mCount = 0;
        int x = 1,y = 1,a,b;
        printf("Press: ");
        int color = board[r1][c1].color;

        while (board[r1][c1 - y].color != color)
        {
            ch = intToCharDif(c1 - y);
            if ((c1 - y) == -1) break;
            printf("%c%d,r1);
            y++;
            mCount++;
        }

        y = 1;

        while (board[r1][c1 + y].color != color)
        {
            ch = intToCharDif(c1 + y);
            if ((c1 + y) == 8) break;
            printf("%c%d,r1);
            y++;
            mCount++;
        }


        x = 1;

        while (board[r1 - x][c1].color != color)
        {
            ch = intToCharDif(c1);
            if ((r1 - x) == -1) break;
            printf("%c%d,r1 - x);
            x++;
            mCount++;
        }

        x = 1;

        while (board[r1 + x][c1].color != color)
        {
            ch = intToCharDif(c1);
            if ((r1 + x) == 8) break;
            printf("%c%d,r1 + x);
            x++;
            mCount++;
        }


        a = 1,r1 - a);
            a++;
            b++;
            mCount++;
        }
        if (mCount == 0)
        {
            printf(" There is no available move!\nTry different piece!\n");
        }
        return mCount;
    }
    int kingMove(int r1,int c1) {
        char ch;
        int mCount = 0;
        int color = board[r1][c1].color;
        printf("Press: ");
        if (board[r1][c1 + 1].color != color) {
            ch = intToCharDif(c1 + 1);
            printf("%c%d,r1);
            mCount++;
        }

        if (board[r1][c1 - 1].color != color) {
            ch = intToCharDif(c1 - 1);
            printf("%c%d,r1);
            mCount++;
        }

        if (board[r1 + 1][c1].color != color) {
            ch = intToCharDif(c1);
            printf("%c%d,r1 + 1);
            mCount++;
        }

        if (board[r1 - 1][c1].color != color) {
            ch = intToCharDif(c1);
            printf("%c%d,r1 - 1);
            mCount++;
        }

        if (board[r1 + 1][c1 + 1].color != color) {
            ch = intToCharDif(c1 + 1);
            printf("%c%d,r1 + 1);
            mCount++;
        }

        if (board[r1 - 1][c1 - 1].color != color) {
            ch = intToCharDif(c1 - 1);
            printf("%c%d,r1 - 1);
            mCount++;
        }

        if (board[r1 - 1][c1 + 1].color != color) {
            ch = intToCharDif(c1 + 1);
            printf("%c%d,r1 - 1);
            mCount++;
        }

        if (board[r1 + 1][c1 - 1].color != color) {
            ch = intToCharDif(c1 - 1);
            printf("%c%d,r1 + 1);
            mCount++;
        }
        
        if (mCount == 0)
        {
            printf(" There is no available move!\nTry different piece!\n");
        }
        return mCount;
    }
    void change(char* ch1,char* ch2)
    {
        int temp1[2] = { 0,0 };
        int temp2[2] = { 0,0 };
        charToInt(ch1,&temp1[0]);
        charToInt(ch2,&temp2[0]);
        piece tempPiece;
        tempPiece.color = 2;
        tempPiece.type = 0;
        board[temp2[1]][temp2[0]] = board[temp1[1]][temp1[0]];
        board[temp1[1]][temp1[0]] = tempPiece;
    }
    void player1()
    {
        int p1[2];
        char ch1[2];
        char ch2[2];
        printf("\nPLAYER 1 (W)\n");
    again1:
        int withMove = 1; //if selected piece has no move this will be 0
        printf("Select Piece: \n");
        scanf("%s",&ch1[0]);
        charToInt(&ch1[0],&p1[0]);
        switch (board[p1[1]][p1[0]].type)
        {
        case 1: 
            withMove = pawnMove(p1[1],p1[0]);
            break;
        case 2: 
            withMove = rookMove(p1[1],p1[0]);
            break;
        case 3: 
            withMove = knightMove(p1[1],p1[0]);
            break;
        case 4: 
            withMove = bishopMove(p1[1],p1[0]);
            break;
        case 5: 
            withMove = queenMove(p1[1],p1[0]);
            break;
        case 6: 
            withMove = kingMove(p1[1],p1[0]);
            break;
        default: printf("Incorrect selection! "); goto again1;
        }
        if (withMove == 0)
            goto again1;
        scanf("%s",&ch2[0]);
        change(&ch1[0],&ch2[0]);
    }
    void player2() {
        int p1[2];
        char ch1[2];
        char ch2[2];
        printf("\nPLAYER 2 (B)\n");
    again1:
        int withMove = 1; //if selected piece has no move this will be 0
        printf("Select Piece: \n");
        scanf("%s",&p1[0]);
        switch (board[p1[1]][p1[0]].type)
        {
        case 1:
            withMove = pawnMove(p1[1],p1[0]);
            break;
        case 2:
            withMove = rookMove(p1[1],p1[0]);
            break;
        case 3:
            withMove = knightMove(p1[1],p1[0]);
            break;
        case 4:
            withMove = bishopMove(p1[1],p1[0]);
            break;
        case 5:
            withMove = queenMove(p1[1],p1[0]);
            break;
        case 6:
            withMove = kingMove(p1[1],&ch2[0]);
    }
};



int main()
{
    Board board = Board();
    char ch;
    printf("\n\tWELCOME TO CHESS GAME");
    printf("\n\n\t By Berkay ");

    _getch();
    system("cls");
    int x = 0;
    do
    {
        x++;
        system("cls");
        board.print();

        if ((x % 2) == 0)
        {
            board.player2();
        }
        else
        {
            board.player1();
        }

        printf(" \n\nPress Enter To Continue ! \n\n ");

        ch = _getch();
    } while (ch == 13);
}

我不确定导致此错误的错误在哪里。这是关于参考资料吗?

解决方法

您发布的代码太长,无法进行详细分析,但我可以看到在 player1()player2() 中您有这样的内容:

char ch1[2];
...
scanf("%s",&ch1[0]);

我只是推测,如果您错误地输入了不止一个字母,那么 ch1 会溢出,因为它只能接受一个字母和结尾 \0

对于只有一个字符相关的信息,与其处理 char[]char *,不如只使用 char? 或者至少你应该用 "%1s" 输入这个字符以防止 char ch1[2] 溢出?

此外,在 charToInt()(在输入之后调用)中,您需要两个字符(char1[0]char1[1]),因此您可能需要声明 char ch1[3]并输入 "%2s" 以存储两个字符和结尾 \0

但是,如果您不知道遇到问题时会发生什么(以及发生在何处),就很难给出更准确的建议。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res