memory - How to solve error process returned -1073741819 in C -
this going minesweeper not finished yet. now, program should ask size of array , number of mines. then, shows hidden board (the 1 numbers , 'x' mines). that's all.
the problem program stop working error
process returned -1073741819 <0xc0000005>
i think it's memory problem, don't know problem is.
sorry, because comments in spanish. i'll try add in capital letters make understandable. code:
main.c
#include <stdio.h> #include <stdlib.h> #include "tablero/tablero.h" #include <stdbool.h> int main() { int fils, cols, nminas; //rows, columns, number of mines printf("introduzca el número de filas: "); scanf("%d", &fils); printf("introduzca el número de columna: "); scanf("%d", &cols); printf("introduzca el número de minas: "); scanf("%d", &nminas); tablero tab = creartablero(fils, cols, nminas);//create board mostrartablero(tab.taboculto, tab.x, tab.y);//show hidden board liberartablero(tab);//free memory return 0; }
tablero.h
#ifndef tablero_h_included #define tablero_h_included typedef struct{ int x;//rows int y;//columns int numminas;//number of mines char **taboculto;//hidden board char **tabvisible;//visible board } tablero; tablero creartablero(int x, int y, int numminas);//create board void mostrartablero(char **tab, int x, int y);//show board void liberartablero(tablero tab);//free board #endif // tablero_h_included
tablero.c
#include "tablero.h" #include <stdio.h> #include<stdlib.h> #include<time.h> //create board tablero creartablero(int x, int y, int numminas){ tablero tab; tab.x = x;//rows tab.y = y;//columns tab.numminas = numminas;//number of mines int i, j, k, l; //iniciamos y establecemos el contenido del tablero que se va ver por consola //initiate , establish content of visible board tab.tabvisible = (char**) malloc (x * sizeof(char*)); for(i = 0; < x; i++) tab.tabvisible[i] = (char*) malloc (y * sizeof(char)); //rellenamos con asteriscos para marcar la casilla normal for(i = 0; < x; i++) for(j = 0; j < y; j++) tab.tabvisible[i][j] = '*'; //iniciamos y establecemos el contenido del tablero oculto, que se irá identificando //initiate , establish content of hidden board tab.taboculto = (char**) malloc (x * sizeof(char*)); for(i = 0; < x; i++) tab.taboculto[i] = (char*) malloc (y * sizeof(char)); //rellenamos con ceros antes de distribuir minas //first, fill '0' for(i = 0; < x; i++) for(j = 0; j < y; j++) tab.taboculto[i][j] = '0'; //introducimos las minas aleatoriamente //establish random mine position srand(time(null)); for(i = 0; < tab.numminas; i++) tab.taboculto[(int)rand() % x][(int)rand() % y] = 'x'; //ponemos los números según las minas que tengan alrededor //put numbers depending on quantity of mines around , position for(i = 0; < x; i++) for(j = 0; j < y; j++) if(tab.taboculto[i][j] == 'x'){ //esquina superior izquierda: de posx, posy posx+1, posy+1 if(i == 0 && j == 0){ for(k = i; k <= i+1; k++) for(l = j; l <= j+1; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //esquina superior derecha: de posx, posy-1 posx+1, posy else if(i == 0 && j == y){ for(k = i; k <= i+1; k++) for(l = j-1; l <= j; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //esquina inferior izquierda: de posx-1, posy posx, posy+1 else if(i == x && j == 0){ for(k = i-1; k <= i; k++) for(l = j; l <= j+1; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //esquina inferior derecha: de posx-1, posy-1 posx, posy else if(i == x && j == y){ for(k = i-1; k <= i; k++) for(l = j-1; l <= j; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //borde superior: de posx, posy-1 posx+1, posy+1 else if(i == 0){ for(k = i; k <= i+1; k++) for(l = j-1; l <= j+1; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //borde inferior: de posx-1, posy-1 posx, posy+1 else if(i == x){ for(k = i-1; k <= i; k++) for(l = j-1; l <= j+1; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //borde izquierdo: de posx-1, posy posx+1, posy+1 else if(j == 0){ for(k = i-1; k <= i+1; k++) for(l = j; l <= j+1; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //borde derecho: de posx-1, posy-1 posx+1, posy else if(j == y){ for(k = i-1; k <= i+1; k++) for(l = j-1; l <= j; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } //sin borde: de posx-1, posy-1 posx+1, posy+1 else{ for(k = i-1; k <= i+1; k++) for(l = j-1; l <= j+1; l++) if(tab.taboculto[k][l] != 'x') tab.taboculto[k][l]++; } } return tab; } //show board void mostrartablero(char **tab, int x, int y){ int i, j; for(i = 0; < x; i++){ for(j = 0; j < y; j++) printf(" %c ", tab[i][j]); printf("\n"); } } //free board void liberartablero(tablero tab){ //liberamos la memoria reservada los arrays int i; for(i = 0; < tab.x; i++){ free(tab.taboculto[i]); free(tab.tabvisible[i]); } free(tab.taboculto); free(tab.tabvisible); }
you 1 off when determine when @ right or bottom borders:
if (j == y)
this can't true, because occurs in loop condition j < y
. right , bottom border cases never entered , end accessing fields @ j + 1
, i + 1
, 1 entry beyond array.
you don't need distinguish between cases @ all. case determines bounds of neighbouring area scan, simple calculation , adjust range when on border:
int jmin = j - 1; int jmax = j + 1; if (j == 0) jmin = 0; if (jmax == y) jmax = y - 1;
and likewise i
. rid of lot of repetitive code.
Comments
Post a Comment