| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1167 人关注过本帖
标题:c++ 内存栈堆错误,已经分配大量内存依然报错
取消只看楼主 加入收藏
denstiny
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-12-8
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
c++ 内存栈堆错误,已经分配大量内存依然报错
程序代码:
#include<iostream>
#include<cstdlib>
#include<pthread.h>
#include<termio.h>
#include<ctime>
#include<unistd.h>
#include <string.h>
#include <sys/select.h>
enum Coordinates
{
    X     = 20,
    Y     = 40,
    score = 40,
};

int Score = 0;
static int map[X][Y] = {0};
static int food[2] = {0};

void *getch(void *ch)
{
    int *sch = (int *)ch;
    char *cch = (char *)ch;
    *sch = getchar();
    while(getchar() != '\n')
        continue;
    return sch;
}
void RandFood()
{
    srand((unsigned)time(0));
    int x,y;
    while((x = 1+rand() % (X-2) ) != food[0] || (y = 1+rand() % (Y-2) ) != food[1])
    {
        food[0] = x;
        food[1] = y;
    }
}

void InitRandCoordinatesSnake(int *coordinates_snake)
{
    srand((unsigned)time(0));
    int i = 0;
    int x = 2+rand() % (X-3);
    int y = 2+rand() % (Y-6);
    *(coordinates_snake+1) = y;
    *(coordinates_snake) = x;
    int s = 0;
    do{
        *(coordinates_snake+i) = x;
        *(coordinates_snake+i+1) = y+=1;
        i+=2;
    }while(i < 2*3);
    food[1] = 1+rand() % (X-2);
    food[0] = 1+rand() % (Y-2);
}
void PrintMap()
{
        std::cout <<"    " << "::贪::吃::蛇::" << std::endl;
        std::cout <<"    " <<"::分数::" <<Score << "::" <<std::endl;
        std::cout <<"    " <<"::长度::" <<Score+3 << "::" << std::endl;
    for(int i = 0; i < X; i++)
{
        for(int k = 0; k < Y;k++)
        {
            if(i == 0 || i == X-1 || k == 0 || k == Y-1)
                std::cout << ":" << "";
            else if(map[i][k] == 99)
                std::cout << " " << "";
            else if(map[i][k] == 55)
                std::cout << "*" << "";
            else if(map[i][k] == 3)
                 std::cout << "O" << "";
            else
                std::cout << "" << "";
        }
        std::cout << std::endl;
    }
}

int  AddHead(int *coordinates_snake,int x,int y) //移动
{
    if(x == 0 || x == X-1 || y == 0 || y == Y-1)
    {
        std::cout << "::游戏结束::" << std::endl;
        std::cout << "!!请注意不要触碰墙壁!!" << std::endl;
        exit(0);
    }
    else if(map[x][y] > 3 && map[x][y] != 99 && map[x][y] != 55)
    {
        std::cout << "::游戏结束::" << std::endl;
        std::cout << "!!请注意不要咬到自己" << std::endl;
        exit(0);
    }

    int i; for(i = 0;*(coordinates_snake+i) != 0 && *(coordinates_snake+i+1) != 0;i+=2);

    for(i+=2;i > -1;i-=2)
    {
        *(coordinates_snake+i-2) = *(coordinates_snake+i-2) ^ *(coordinates_snake+i);
        *(coordinates_snake+i)   = *(coordinates_snake+i-2) ^ *(coordinates_snake+i);
        *(coordinates_snake+i-2) = *(coordinates_snake+i-2) ^ *(coordinates_snake+i);

        *(coordinates_snake+i-3) = *(coordinates_snake+i-3) ^ *(coordinates_snake+i-1);
        *(coordinates_snake+i-1) = *(coordinates_snake+i-3) ^ *(coordinates_snake+i-1);
        *(coordinates_snake+i-3) = *(coordinates_snake+i-3) ^ *(coordinates_snake+i-1);
    }
    *coordinates_snake = x,*(coordinates_snake+1) = y;
    if(x == food[0] && y == food[1])
    {
        RandFood();
        return 1;
    }
    return 0;
}

void Clear()
{
    system("clear");
}
void DeleteTheLast(int *coordinates_snake)
{
    int i; for(i = 0;*(coordinates_snake+i+2) != 0 && *(coordinates_snake+i+3) != 0;i+=2);
    map[i][i+1] = 99;
    *(coordinates_snake+i) = 0,*(coordinates_snake+i+1) = 0;
}

void  SetMap(int *coordinates_snake)
{ 
    int i;
    for(i = 0; i < X; i++)
        for(int k = 0; k < Y;k++)
            map[i][k]={99};
    for(i = 0;*(coordinates_snake+i) != 0 && *(coordinates_snake+i+1) != 0;i+=2)
    {
        if(Score < 10)
            map[*(coordinates_snake+i)][*(coordinates_snake+i+1)] = i/2+3;
        else
        {
            Clear();
            std::cout << "::恭喜通关::" << std::endl;
            exit(0);
        }

    }
    for(int i = 0;i < 2;i++)
        map[food[0]][food[1]] = 55;

}

int main(int argc,char *argv[])
{
    pthread_t th;
    int *coordinates_snake = new int[20000]; //0:1 x y 
    int *a = new int,al = 's'; *a = 'a';
    InitRandCoordinatesSnake(coordinates_snake);                 //初始化地图
    RandFood();

    SetMap(coordinates_snake);                                                     //将值带入地图中
    PrintMap();

    while(1)
    {
        int f = 0;
        Clear();
        PrintMap();                                                                 //打印地图
        pthread_create(&th,NULL,getch,a);
        usleep(300000);
        if(*a == 'a' && al != 'd' || *a == 'd' && al != 'a' ||
                *a == 'w' && al != 's' || *a == 's' && al != 'w' || *a == 'q')
        {
            al = *a;
            switch(*a)
            {
                case 'a': if(AddHead(coordinates_snake,*(coordinates_snake),*(coordinates_snake+1)-1) == 1) f++; break;
                case 'd': if(AddHead(coordinates_snake,*(coordinates_snake),*(coordinates_snake+1)+1) == 1) f++; break;
                case 'w': if(AddHead(coordinates_snake,*(coordinates_snake)-1,*(coordinates_snake+1)) == 1) f++; break;
                case 's': if(AddHead(coordinates_snake,*(coordinates_snake)+1,*(coordinates_snake+1)) == 1) f++; break;
                default:exit(0);
            }
            if(f == 0)
                DeleteTheLast(coordinates_snake);                                             //清理尾巴
            else
            {
                Score++;
                RandFood();
            }
            SetMap(coordinates_snake);                                                     //将值带入地图中
        }
    }
    delete a;
    delete [] coordinates_snake;
    return 0;
}


程序运行到第八的时候就会出现段错误,gdb调试的时候会出现通关成功,无段错误提示,
本来以为是coordinates_snake 内存分配小了,但是还是不行,真的不知道是哪里出了问题了
搜索更多相关主题的帖子: int map cout std || 
2020-12-08 00:08
denstiny
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-12-8
收藏
得分:0 
好爱这个论坛,比csdn环境好多了
2020-12-08 00:12
denstiny
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-12-8
收藏
得分:0 
之前的帐号密码忘记了,唉重新创了一个,有没有大佬帮我看看
2020-12-08 00:13
denstiny
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-12-8
收藏
得分:0 
我就是不晓得哪里出了问题呀,大佬嗯帮我看看嘛
2020-12-08 08:13
快速回复:c++ 内存栈堆错误,已经分配大量内存依然报错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046761 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved