数独生成器导致分段错误
Sudoku Generator causes segmentation fault
我正在尝试用 C 编写一个生成随机数独的程序。它生成随机数并检查行中或列中或正方形 3x3 中是否存在相同的数字,如果不是,则将其放入下一个单元格 e 中。唯一的问题是第 5 行,当索引为 6 时,它给出了 Segmentation Fault。如果我在程序中评论时更改,它会进入循环。怎么了?
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 *
或从 void *
转换。然后自动完成转换。
另一方面,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,指针 space 所需的内存大小在很大程度上取决于目标可执行文件,即 32 位或 64 位,但在这种情况下,sizeof(*sudo)
将适用于任何一个.
我正在尝试用 C 编写一个生成随机数独的程序。它生成随机数并检查行中或列中或正方形 3x3 中是否存在相同的数字,如果不是,则将其放入下一个单元格 e 中。唯一的问题是第 5 行,当索引为 6 时,它给出了 Segmentation Fault。如果我在程序中评论时更改,它会进入循环。怎么了?
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 *
或从 void *
转换。然后自动完成转换。
另一方面,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,指针 space 所需的内存大小在很大程度上取决于目标可执行文件,即 32 位或 64 位,但在这种情况下,sizeof(*sudo)
将适用于任何一个.