0

このコードは大きなソース コードの一部ですが、バグはここにあります。

int solve(int* unsolved_var)
{
        int i;
        for (i = 0; unsolved_var[i] != 0; i++)
        {
            int n;
            for (n = 1; n <= 9; n++)
            {
                 //if (!numInBox(unsolved_var[i], n)==1 || !numInLine()==1)

                 printf("%d  %d\n", n,  unsolved_var[i]);
         numInBox(unsolved_var[i], n);//ERROR OCCURS HERE
            }
        }
}

int numInBox(int space, int n)//space is always three digits long
{
   printf("this should print but doesnt(segmentation fault)");
   char unsolved_str[3];
   sprintf(unsolved_str, "%d", space);//converts int into char[]

   printf("%s",unsolved_str);//confirms

   int check[3] = {atoi(unsolved_str[0]), 0, atoi(unsolved_str[2])};//converts char[] to int[]

完全なコード...その長い間、あなたは正常に動作します。solve と入力すると、そのエラーがスローされます。

`#include <stdio.h>
#include <string.h>

char grid[20][42];

void display()
{
        int j;
    int u;
    for (j = 0; j <= 19; j++)
    {
        for (u = 0; u <= 39; u++)
        {
            printf("%c",grid[j][u]);
        }
        printf("\n");
    }
    printf("\n");
}

int* unsolved()
{//2-18 4,8-36
int r;
int c;
   int size = 0;
   static int unsolved_var[81];
   unsolved_var[0] = 0;
   for (r = 2; r <= 18; r+=2)
   {
       for (c = 4; c <=36; c+=4)
       {
           if (grid[r][c] == ' ')
           {
           if (c <= 9)
               {
                   unsolved_var[size] = r/2*100 + c/4;
                   size++;
                   unsolved_var[size] = 0;//sets end of array
               }
               else
               {
                   unsolved_var[size] = r/2*100 + c/4;
                   size++;
                   unsolved_var[size] = 0;
               }
        // 100   1000    100   1000
            //2-0-4 2-0-10 10-0-4 10-0-10
           }
       }
   }
int p;
//for (p = 0; p <= size; p++){printf("%d", unsolved_var[p]);}
return unsolved_var;
}

int numInBox(int space, int n)
{
   char unsolved_str[3];
   sprintf(unsolved_str, "%d", space);

   printf("%s",unsolved_str);

   int check[3] = {atoi(unsolved_str[0]), 0, atoi(unsolved_str[2])};

}//***********************************************************************

static int x = 0;
int solve(int* unsolved_var)
{
   if (unsolved_var[0] == 0 || x == 10){return 0;}
   else
   {
        int i;
        for (i = 0; unsolved_var[i] != 0; i++)
        {
            int n;
            for (n = 1; n <= 9; n++)
            {
                 //if (!numInBox(unsolved_var[i], n)==1 || !numInLine()==1)

                 printf("%d  %d\n", n,  unsolved_var[i]);//
         numInBox(unsolved_var[i], n);
            }
        }
    x++;
   }
}

main()
{
//*****************************GRID******************************
//2 4 6 8 10 12 14 16 18
//3-5 7-9 11-13 15-17 19-21 23-25 27-29 31-33 35-37
        char top[] =   "\t  1   2   3   4   5   6   7   8   9  ";
    char line0[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field1[]="1\t|   |   |   |   |   |   |   |   |   |";
    char line1[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field2[]="2\t|   |   |   |   |   |   |   |   |   |";
    char line2[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field3[]="3\t|   |   |   |   |   |   |   |   |   |";
    char line3[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field4[]="4\t|   |   |   |   |   |   |   |   |   |";
    char line4[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field5[]="5\t|   |   |   |   |   |   |   |   |   |";
    char line5[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field6[]="6\t|   |   |   |   |   |   |   |   |   |";
    char line6[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field7[]="7\t|   |   |   |   |   |   |   |   |   |";
    char line7[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field8[]="8\t|   |   |   |   |   |   |   |   |   |";
    char line8[] = "\t+---+---+---+---+---+---+---+---+---+";
    char field9[]="9\t|   |   |   |   |   |   |   |   |   |";
    char line9[] = "\t+---+---+---+---+---+---+---+---+---+";

        int o;
        for (o = 0; o <= 39; o++)
        {
            grid[0][o] = top[o];
        grid[1][o] = line0[o];
        grid[2][o] = field1[o];
        grid[3][o] = line1[o];
        grid[4][o] = field2[o];
        grid[5][o] = line2[o];
        grid[6][o] = field3[o];
        grid[7][o] = line3[o];
        grid[8][o] = field4[o];
        grid[9][o] = line4[o];
        grid[10][o] = field5[o];
        grid[11][o] = line5[o];
        grid[12][o] = field6[o];
        grid[13][o] = line6[o];
        grid[14][o] = field7[o];
        grid[15][o] = line7[o];
        grid[16][o] = field8[o];
        grid[17][o] = line8[o];
        grid[18][o] = field9[o];
        grid[19][o] = line9[o];
        }
    display();
//****************************INPUT******************************
while(1)
{
  char input_raw[7];
  printf("Enter a Command\n");
  fgets(input_raw, 256, stdin);
  input_raw[strlen(input_raw)-1] = '\0';

if (strcmp(input_raw, "solve") == 0){solve(unsolved());}
else
{
  char *first = input_raw;
  char *second = input_raw; 
  char *third = input_raw;

  char *copy = input_raw;

  int flag = 1;
  while (*copy) 
  {
    if (*copy == ',') 
    {
      *copy = '\0'; 
      if (flag==0) {third = ++copy;}
      if (flag==1) {second = ++copy; flag = 0;}

    }
    else copy++;
  }

  switch (first[0])
  {
      case '1':
      {
          switch (second[0])
          {
              case '1':{grid[2][3] = '*'; grid[2][4]=third[0]; grid[2][5] = '*'; break;}
              case '2':{grid[2][7] = '*'; grid[2][8]=third[0]; grid[2][9] = '*'; break;}
              case '3':{grid[2][11] = '*';grid[2][12]=third[0];grid[2][13] = '*';break;}
              case '4':{grid[2][15] = '*';grid[2][16]=third[0];grid[2][17] = '*';break;}
              case '5':{grid[2][19] = '*';grid[2][20]=third[0];grid[2][21] = '*';break;}
              case '6':{grid[2][23] = '*';grid[2][24]=third[0];grid[2][25] = '*';break;}
              case '7':{grid[2][27] = '*';grid[2][28]=third[0];grid[2][29] = '*';break;}
              case '8':{grid[2][31] = '*';grid[2][32]=third[0];grid[2][33] = '*';break;}
              case '9':{grid[2][35] = '*';grid[2][36]=third[0];grid[2][37] = '*';break;}
          }
          break;
      }
      case '2':
      {
          switch (second[0])
          {
              case '1':{grid[4][3] = '*'; grid[4][4]=third[0]; grid[4][5] = '*'; break;}
              case '2':{grid[4][7] = '*'; grid[4][8]=third[0]; grid[4][9] = '*'; break;}
              case '3':{grid[4][11] = '*';grid[4][12]=third[0];grid[4][13] = '*';break;}
              case '4':{grid[4][15] = '*';grid[4][16]=third[0];grid[4][17] = '*';break;}
              case '5':{grid[4][19] = '*';grid[4][20]=third[0];grid[4][21] = '*';break;}
              case '6':{grid[4][23] = '*';grid[4][24]=third[0];grid[4][25] = '*';break;}
              case '7':{grid[4][27] = '*';grid[4][28]=third[0];grid[4][29] = '*';break;}
              case '8':{grid[4][31] = '*';grid[4][32]=third[0];grid[4][33] = '*';break;}
              case '9':{grid[4][35] = '*';grid[4][36]=third[0];grid[4][37] = '*';break;}
          }
          break;
      }
      case '3':
      {
          switch (second[0])
          {
              case '1':{grid[6][3] = '*'; grid[6][4]=third[0]; grid[6][5] = '*'; break;}
              case '2':{grid[6][7] = '*'; grid[6][8]=third[0]; grid[6][9] = '*'; break;}
              case '3':{grid[6][11] = '*';grid[6][12]=third[0];grid[6][13] = '*';break;}
              case '4':{grid[6][15] = '*';grid[6][16]=third[0];grid[6][17] = '*';break;}
              case '5':{grid[6][19] = '*';grid[6][20]=third[0];grid[6][21] = '*';break;}
              case '6':{grid[6][23] = '*';grid[6][24]=third[0];grid[6][25] = '*';break;}
              case '7':{grid[6][27] = '*';grid[6][28]=third[0];grid[6][29] = '*';break;}
              case '8':{grid[6][31] = '*';grid[6][32]=third[0];grid[6][33] = '*';break;}
              case '9':{grid[6][35] = '*';grid[6][36]=third[0];grid[6][37] = '*';break;}
          }
          break;
      }
      case '4':
      {
          switch (second[0])
          {
              case '1':{grid[8][3] = '*'; grid[8][4]=third[0]; grid[8][5] = '*'; break;}
              case '2':{grid[8][7] = '*'; grid[8][8]=third[0]; grid[8][9] = '*'; break;}
              case '3':{grid[8][11] = '*';grid[8][12]=third[0];grid[8][13] = '*';break;}
              case '4':{grid[8][15] = '*';grid[8][16]=third[0];grid[8][17] = '*';break;}
              case '5':{grid[8][19] = '*';grid[8][20]=third[0];grid[8][21] = '*';break;}
              case '6':{grid[8][23] = '*';grid[8][24]=third[0];grid[8][25] = '*';break;}
              case '7':{grid[8][27] = '*';grid[8][28]=third[0];grid[8][29] = '*';break;}
              case '8':{grid[8][31] = '*';grid[8][32]=third[0];grid[8][33] = '*';break;}
              case '9':{grid[8][35] = '*';grid[8][36]=third[0];grid[8][37] = '*';break;}
          }
          break;
      }
      case '5':
      {
          switch (second[0])
          {
              case '1':{grid[10][3] = '*'; grid[10][4]=third[0]; grid[10][5] = '*'; break;}
              case '2':{grid[10][7] = '*'; grid[10][8]=third[0]; grid[10][9] = '*'; break;}
              case '3':{grid[10][11] = '*';grid[10][12]=third[0];grid[10][13] = '*';break;}
              case '4':{grid[10][15] = '*';grid[10][16]=third[0];grid[10][17] = '*';break;}
              case '5':{grid[10][19] = '*';grid[10][20]=third[0];grid[10][21] = '*';break;}
              case '6':{grid[10][23] = '*';grid[10][24]=third[0];grid[10][25] = '*';break;}
              case '7':{grid[10][27] = '*';grid[10][28]=third[0];grid[10][29] = '*';break;}
              case '8':{grid[10][31] = '*';grid[10][32]=third[0];grid[10][33] = '*';break;}
              case '9':{grid[10][35] = '*';grid[10][36]=third[0];grid[10][37] = '*';break;}
          }
          break;
      }
      case '6':
      {
          switch (second[0])
          {
              case '1':{grid[12][3] = '*'; grid[12][4]=third[0]; grid[12][5] = '*'; break;}
              case '2':{grid[12][7] = '*'; grid[12][8]=third[0]; grid[12][9] = '*'; break;}
              case '3':{grid[12][11] = '*';grid[12][12]=third[0];grid[12][13] = '*';break;}
              case '4':{grid[12][15] = '*';grid[12][16]=third[0];grid[12][17] = '*';break;}
              case '5':{grid[12][19] = '*';grid[12][20]=third[0];grid[12][21] = '*';break;}
              case '6':{grid[12][23] = '*';grid[12][24]=third[0];grid[12][25] = '*';break;}
              case '7':{grid[12][27] = '*';grid[12][28]=third[0];grid[12][29] = '*';break;}
              case '8':{grid[12][31] = '*';grid[12][32]=third[0];grid[12][33] = '*';break;}
              case '9':{grid[12][35] = '*';grid[12][36]=third[0];grid[12][37] = '*';break;}
          }
          break;
      }
      case '7':
      {
          switch (second[0])
          {
              case '1':{grid[14][3] = '*'; grid[14][4]=third[0]; grid[14][5] = '*'; break;}
              case '2':{grid[14][7] = '*'; grid[14][8]=third[0]; grid[14][9] = '*'; break;}
              case '3':{grid[14][11] = '*';grid[14][12]=third[0];grid[14][13] = '*';break;}
              case '4':{grid[14][15] = '*';grid[14][16]=third[0];grid[14][17] = '*';break;}
              case '5':{grid[14][19] = '*';grid[14][20]=third[0];grid[14][21] = '*';break;}
              case '6':{grid[14][23] = '*';grid[14][24]=third[0];grid[14][25] = '*';break;}
              case '7':{grid[14][27] = '*';grid[14][28]=third[0];grid[14][29] = '*';break;}
              case '8':{grid[14][31] = '*';grid[14][32]=third[0];grid[14][33] = '*';break;}
              case '9':{grid[14][35] = '*';grid[14][36]=third[0];grid[14][37] = '*';break;}
          }
          break;
      }
      case '8':
      {
          switch (second[0])
          {
              case '1':{grid[16][3] = '*'; grid[16][4]=third[0]; grid[16][5] = '*'; break;}
              case '2':{grid[16][7] = '*'; grid[16][8]=third[0]; grid[16][9] = '*'; break;}
              case '3':{grid[16][11] = '*';grid[16][12]=third[0];grid[16][13] = '*';break;}
              case '4':{grid[16][15] = '*';grid[16][16]=third[0];grid[16][17] = '*';break;}
              case '5':{grid[16][19] = '*';grid[16][20]=third[0];grid[16][21] = '*';break;}
              case '6':{grid[16][23] = '*';grid[16][24]=third[0];grid[16][25] = '*';break;}
              case '7':{grid[16][27] = '*';grid[16][28]=third[0];grid[16][29] = '*';break;}
              case '8':{grid[16][31] = '*';grid[16][32]=third[0];grid[16][33] = '*';break;}
              case '9':{grid[16][35] = '*';grid[16][36]=third[0];grid[16][37] = '*';break;}
          }
          break;
      }
      case '9':
      {
          switch (second[0])
          {
              case '1':{grid[18][3] = '*'; grid[18][4]=third[0]; grid[18][5] = '*'; break;}
              case '2':{grid[18][7] = '*'; grid[18][8]=third[0]; grid[18][9] = '*'; break;}
              case '3':{grid[18][11] = '*';grid[18][12]=third[0];grid[18][13] = '*';break;}
              case '4':{grid[18][15] = '*';grid[18][16]=third[0];grid[18][17] = '*';break;}
              case '5':{grid[18][19] = '*';grid[18][20]=third[0];grid[18][21] = '*';break;}
              case '6':{grid[18][23] = '*';grid[18][24]=third[0];grid[18][25] = '*';break;}
              case '7':{grid[18][27] = '*';grid[18][28]=third[0];grid[18][29] = '*';break;}
              case '8':{grid[18][31] = '*';grid[18][32]=third[0];grid[18][33] = '*';break;}
              case '9':{grid[18][35] = '*';grid[18][36]=third[0];grid[18][37] = '*';break;}
          }
          break;
      }
  }
}
display();
}
}
4

5 に答える 5

3

numInBox 最初に(少なくとも) 2 つの問題がありunsolved_str、nul 終了の余地がないため、スタックに落書きしています。Secondatoiは を受け取りますが、引数char*を与えています。char(少なくともそれについて警告を受けているはずです。)それはほぼ確実に内部でセグメンテーション違反を引き起こしatoiます。

于 2011-05-19T04:25:54.253 に答える
1

コード全体を表示することはできませんが、定義char unsolved_str[3];して、おそらくint solve(int* unsolved_var). したがって、int *引数として char 配列を渡しました。

またsolve、ループ内で、 aが見つからないforときに配列をスキャンします。0配列に 0 がない場合、ループは配列範囲を超えます。forしたがって、ループ内の境界条件のテストとして、配列の長さも含めます。

再び内側forのループで、ループをi=1toで繰り返しました9。配列の境界を確認し、渡された配列に要素 index にアクセスするのに十分なスペースが割り当てられているかどうかを確認してください9

于 2011-05-19T04:24:15.473 に答える
1

文字列ヌル ターミネータのための余地を残しておく必要があります。3 文字ではなく 4 文字を使用してください。

于 2011-05-19T04:24:49.193 に答える
1

バッファーであるためprintf()、セグメンテーション違反が発生した正確な行のインジケーターとして使用することは、一般的には良い考えではありません。一般に、 への書き込みは への書き込みstderrのようにバッファリングしませんstdout。そのため、クラッシュする前にどれだけの距離を取得したかを確認する場合は、次のようなことを行う必要があります。fprintf(stderr, "Got Here before crash...\n");

そうは言っても、あなたの本当の問題はラインにあります

int check[3] = {atoi(unsolved_str[0]), 0, atoi(unsolved_str[2])};

コンパイラの警告を見ると、次のようになっているはずです。

warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast
/usr/include/stdlib.h:148: note: expected ‘const char *’ but argument is of type ‘char’

type のオブジェクトではなく、に定数ポインタcharを渡すように、その行を書き直す必要があります。したがって、アドレス取得演算子を追加してこれを試してください。atoi()char

int check[3] = {atoi(&unsolved_str[0]), 0, atoi(&unsolved_str[2])};

atoiこれで、期待されている適切なポインター型が渡されます。

于 2011-05-19T04:30:42.517 に答える
0

この部分は心配です:

char unsolved_str[3];
sprintf(unsolved_str, "%d", space);//converts int into char[]

unsolved_strifspaceが実際に 3 桁の場合、NULL ターミネータ用のスペースはありません。4 番目の文字のためのスペースが必要です。

于 2011-05-19T04:26:56.570 に答える