c - 数独生成器导致段错误

标签 c segmentation-fault sudoku

我正在尝试用 C 编写一个程序来生成随机数独。它生成随机数并检查行、列或 3x3 正方形中是否有相同的数字,如果没有,则将其放入单元格 e 中,然后转到下一个单元格。唯一的问题是第 5 行,当索引为 6 时,会出现段错误。如果我在程序中评论时进行更改,它就会进入循环。怎么了?

include <string.h>
#include <stdlib.h>
#include "sudoku.h"
#include <stdio.h>
#include <time.h>

int dimension = 9;

int main(int argc, char** argv){
  int dimension = 9;
  int j ,k ;
  int ** sudo =  malloc(sizeof(*sudo)*dimension);
  for ( j = 0; j< dimension; j++){
    sudo[j] = malloc(sizeof(int)*dimension);
    for ( k = 0; k<dimension; k++){
      sudo[j][k] =0;

    }
  }
  riempiSudoku(sudo);
  return 0;


}

void riempiSudoku(int** sudo){  //fill sudoku
  int i,j;
  srand ( time(NULL));
  srand(rand());
  for (i=0;i<dimension;i++){
    for(j=0;j<dimension;j++){
      int ran;
      do
    ran= rand() %9 ;
      while(checkSquare(sudo,i,j,ran+1)||checkRow(sudo,i,ran+1)
        ||checkCol(sudo,j,ran+1));
      sudo[i][j] = ran+1;
      printf("%d", sudo[i][j]);
    }
    printf("\n");
  }
}


int checkRow(int** sudo, int row, int value){ //check if the number is in the row
  int i;
  for (i = 0; i<dimension; i++){
    if (sudo[row][i] == value)
      return 1;
  }
  return 0;
}

int checkCol(int** sudo, int col, int value){//check if the number is in the col
  int i;
  for (i = 0; i<dimension; i++){
    if (sudo[i][col] == value)
      return 1;
  }
  return 0;

}

int checkSquare(int** sudo, int row, int col, int value){ //check if the number is in the square 3x3
  int i,j;
  if (row==0||row==2||row==1){
    if(col==0||col==1||col==2){
      for(i=0;i<3;i++){
    for(j=0;j<3;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
    if(col==3||col==4||col==5){
      for(i=0;i<3;i++){
    for(j=3;j<6;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
    if(col==6||col==7||col==8){
      for(i=0;i<3;i++){
    for(j=6;j<9;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
  }
  if (row==3||row==4||row==5){
    if(col==0||col==1||col==2){
      for(i=3;i<6;i++){
    for(j=0;j<3;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
    if(col==3||col==4||col==5){
      for(i=3;i<6;i++){
    for(j=3;j<6;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
    if(col==6||col==7||col==8){
      for(i=3;i<6;i++){
    for(j=6;j<9;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
  }
  if (row==6||row==7||row==8){
    if(col==0||col==1||col==2){
      for(i=6;i<9;i++){
    for(j=0;j<3;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
    if(col==3||col==4||col==5){
      for(i=6;i<9;i++){
    for(j=3;j<6;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
    if(col==6||col==7||col==8){
      for(i=6;i<9;i++){
    for(j=6;j<9;j++){
      if (sudo[i][j] == value)
        return 1;
    }
      }
      return 0;
    }
  }


}

最佳答案

In C, it is not correct to cast the return of [m][c][re]alloc()
C99(或我所知道的任何其他 C 版本)不需要强制转换。 C 隐式地与 void * 进行转换。然后 Actor 就会自动完成。
另一方面,C++ 需要强制转换,因为它只会将转换为 void *,而不

对于初学者,请更改代码的这一部分:

 int ** sudo = (int**) malloc(sizeof(int)*dimension);
  for ( j = 0; j< dimension; j++){
    sudo[j] = (int*) malloc(sizeof(int)*dimension);  

致:

 int ** sudo = malloc(sizeof(*sudo)*dimension);
  for ( j = 0; j< dimension; j++){
    sudo[j] = malloc(sizeof(int)*dimension);  

注意:对于第一个 malloc,指针空间所需的内存大小非常依赖于目标可执行文件,即 32 位或 64 位,但在这种情况下 sizeof(*sudo) 将适用于任何一个。

关于c - 数独生成器导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161593/

相关文章:

c - 在c中的数组中查找一个字符

java - 回溯时数独递归有问题。 (蛮力)

javascript - 递归:JS 中的回溯数独求解器

list - 在 Haskell 中输出列表列表?

c - 如何在C中将整数转换为字符串?

c - libcurl C API > POST 从 URL 获取的数据范围

c++ - 将 4 个原始字节转换为 32 位 float

c - C 中的强制转换结构中的段错误

c - 我不明白为什么会收到 "Segmentation fault (core dumped)"错误

c - 段错误(转储核心)