简体   繁体   English

二维数组中的分段错误

[英]segmentation fault in 2d array

I am writing the code about the lcs algorithm.我正在编写有关 lcs 算法的代码。 Because of the condition, I made the 2d lcs_table for the lcs algorithm by using malloc function.由于这种情况,我使用 malloc 函数为 lcs 算法制作了 2d lcs_table。 While the initializing the array as 0, there is segmentation fault.在将数组初始化为 0 时,存在分段错误。 I don't know where is wrong..不知道哪里错了。。

void findLCS(char lcs_1[], const char s1[], const char s2[])
{
  /* FILL */
  int i,j,k,l;
  int s1_len = strlen(s1);
  int s2_len = strlen(s2);
  // int LCS_array[s1_len+1][s2_len+1];  //>>malloc use
  //printf("%d, %d\n",s1_len, s2_len);
  int **LCS_array;
  LCS_array = malloc(sizeof(char)*(s1_len+1));
  for(i=0;i<=s1_len;i++){
    LCS_array[i] = malloc(sizeof(char)*(s2_len+1));
    if(LCS_array[i]==NULL){
      perror("malloc:");
      exit(1);
    }
  }
  printf("s1_len : %d, s2_len : %d\n",s1_len,s2_len);
  for (i = 0 ; i <=s1_len;i++){
    for (j = 0 ; j <=s2_len;j++){
        printf("[%d,%d] ",i,j);
        LCS_array[i][j] = 0;
    }
    printf("\n");
  }

  for(i = 1 ;i<=s1_len;i++){
      for(j = 1;j<=s2_len;j++){
          if (s1[i-1]==s2[j-1]){
            LCS_array[i][j] =(LCS_array[i-1][j-1]+1);
          }
          else if(LCS_array[i-1][j]>=LCS_array[i][j-1]){
            LCS_array[i][j] =LCS_array[i-1][j];
          }
          else{
            LCS_array[i][j]=LCS_array[i][j-1];
          }
      }
  }

  int index = LCS_array[s1_len][s2_len];
  lcs_1[index] = '\0';

  i = s1_len, j = s2_len;
  while (i > 0 && j > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      lcs_1 [index-1] = s1[i-1];
      i--;
      j--;
      index--;
    }
    else if (LCS_array[i - 1][j] < LCS_array[i][j - 1])
      j--;
    else
      i--;
  } 
}
jeon@ubuntu:~/hw5$ ./hw5_cor ccgtttcca tgcccatct
$ ./hw5_cor ccgtttcca tgcccatct 
s1_len : 9, s2_len : 9
[0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7] [0,8] [0,9] 
[1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7] [1,8] [1,9] 
[2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9] 
[3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] [3,7] [3,8] [3,9] 
Segmentation fault (core dumped)

Where do I correct the code?我在哪里更正代码?

The LCS_array = malloc(sizeof(char)*(s1_len+1)); LCS_array = malloc(sizeof(char)*(s1_len+1)); should be LCS_array = malloc(sizeof(char*)*(s1_len+1));应该是LCS_array = malloc(sizeof(char*)*(s1_len+1)); with an additional asterisk after char keyword.在 char 关键字后加上一个星号。 This is because LCS_array is a pointer of pointers, in other words it should be an array in which each element is also a pointer to an integer array.这是因为LCS_array是一个指针的指针,换句话说它应该是一个数组,其中每个元素也是一个指向整数数组的指针。

Refer to this article from Geeksforgeeks for more info about creating 2D array in C. I think this should solve the segmentation fault problem (I'm not sure because I'm on windows).有关在 C 中创建 2D 数组的更多信息,请参阅 Geeksforgeeks 的 这篇文章。我认为这应该可以解决分段错误问题(我不确定,因为我在 Windows 上)。

Here's an example on how to create a 2D array dynamically in C from the GFG article:这是 GFG 文章中有关如何在 C 中动态创建 2D 数组的示例:

    int r = 3, c = 4, i, j;
 
    int** arr = (int**)malloc(r * sizeof(int*));
    for (i = 0; i < r; i++)
        arr[i] = (int*)malloc(c * sizeof(int));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM