HOW TO FIX "0xC0000005" error in the following code in c++?

-5

I am making a simple snake game in c++ using linked list.The code was working fine but suddenly it started crashing with this error :

" Process returned -1073741819 (0xC0000005) execution time : 2.301 s Press any key to continue. "

I searched about it on other forums and realized the it caused because I tried to access restricted memory. I want to know what caused this error and how can I get it fixed ? Below is code giving the error :

#include <iostream>
#include<string.h>
#include<malloc.h>
#include<windows.h>
#include<stdlib.h>

using namespace std;

struct Node
{
    string data;
    Node* link;
    int x_cor;
    int y_cor;
};

void createMap(string map[25][25]);
void printMap(string map[25][25]);
void createSnake(Node** head,Node** tail,string map[25][25]);
void pushNode(struct Node** head,Node** tail,string map[25][25]);

void ShowConsoleCursor(bool x)
{
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    GetConsoleCursorInfo(out,&info);
    info.bVisible = x;
    SetConsoleCursorInfo(out,&info);
}

int main()
{
    string map[25][25];
    Node* head;
    Node* tail;
    ShowConsoleCursor(false);
    createMap(map);
    createSnake(&head,&tail,map);
    printMap(map);

    for(int i=0;i<10;i++)
    {
        system("CLS");
        pushNode(&head,&tail,map);
        printMap(map);
        Sleep(500);
    }

    cout<<head->x_cor<<" ";
    cout<<head->y_cor;
    return 0;
}

void createMap(string map[25][25])
{
    for(int i=0;i<25;i++)
    {
        for(int j=0;j<25;j++)
        {
            if((i==0)||(i==24)||(j==0)||(j==24))
                map[i][j] = "# ";
            else
                map[i][j] = "  ";
        }
    }
}

void printMap(string map[25][25])
{
    cout<<"\n";
    for(int i=0;i<25;i++)
    {
        cout<<"  ";
        for(int j=0;j<25;j++)
        {
            cout<<map[j][i];
        }
        cout<<endl;
    }
}

void createSnake(Node** head,Node** tail,string map[25][25])
{
    Node* temp;
    Node* newNode;
    for(int i=2;i<7;i++)
    {
        newNode =(Node*)malloc(sizeof(Node));
        newNode->x_cor = i;
        newNode->y_cor = 3;
        newNode->data = "O ";
        if(i==2)
        {
            (*tail) = newNode;
            temp = newNode;
        }
        if(i == 6)
        {
            (*head) = newNode;
        }
        if(i>0)
        {
            temp->link = newNode;
            temp = temp->link;
        }
        map[newNode->x_cor][newNode->y_cor] = newNode->data;
    }
    free(temp);
    free(newNode);
}

void pushNode(struct Node** head,Node** tail,string map[25][25])
{
    Node* newNode =(Node*)malloc(sizeof(Node));
    newNode->data = "O ";
    newNode->x_cor = (*head)->x_cor;
    newNode->y_cor = (*head)->y_cor;
    (*head)->link = newNode;
    (*head) = (*head)->link;
    ((*head)->x_cor)++;
    map[(*head)->x_cor][(*head)->y_cor] = newNode->data;
    int x = (*tail)->x_cor;
    int y = (*tail)->y_cor;
    Node* temp;
    temp = (*tail);
    (*tail) = (*tail)->link;
    free(temp);
    map[x][y] = "  ";
    free(newNode);
    free(temp);
}



c++
asked on Stack Overflow Sep 22, 2019 by Ramesh Dalvi • edited Sep 22, 2019 by Remy Lebeau

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0