开发者

calling method fail: segmentation fault in c

This piece of code is part of a large source code but the bug is here.

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[]

Complete Code...its long it runs fine thou. If you type in solve then ill throw you that error.

`#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] = '*'; b开发者_开发问答reak;}
              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();
}
}


There's (at least) two problems in numInBox First, unsolved_str doesn't have room for the nul termination, so you are scribbling on the stack. Second atoi takes a char*, but you are giving it a char argument. (Should have gotten at least a warning about that.) That will almost certainly give a segmentation fault inside atoi.


Since printf() buffers, using it as an indicator of exactly what line your segmentation fault occurs on is not generally a good idea. In general, writes to stderr don't buffer like they do to stdout, so if you are looking to see how far you've gotten before a crash, you should do something more like fprintf(stderr, "Got Here before crash...\n");

That being said, your real problem is in the line

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

If you look at your compiler warnings, you should be seeing something like:

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’

You need to re-write that line so that you're passing constant pointers to char to atoi(), not objects of type char. So try this by adding an address-of operator:

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

That will now pass the proper pointer type that atoi is expecting.


We cannot see the entire code but i think that you have defined char unsolved_str[3]; and probably have passed it to the int solve(int* unsolved_var). So you have passed a char array as an int * argument.

Also in the solve in the for loop you scan the array while a 0 is not found. If the array does not have a 0, then the loop will go beyond the array range. So include the array length also, as a test of boundary condition in the for loop.

Again in the inside for loop you have iterated the loop with i=1 to 9 . Please check the array boundaries, and if you have enough allocated space in the passed array to access the element index 9.


You must leave room for the string null terminator. Use 4 characters, not 3.


This part is worrying:

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

There's no space for a NULL terminator in unsolved_str if space is indeed three digits. You need room for a fourth character.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜