程序崩溃,我不明白为什么

Program crashes and I can't figure out why

程序崩溃,我不知道为什么。 ptr 和 ptr1 是 char** 并且 ptr 和 ptr1 的每个元素都是 char*,不是所有的 malloc'd space 如果已满,然而,在每个元素中留下一些空指针。我认为问题出在这个部分,因为在崩溃之前打印的最后一件事是在这个部分。

 while (strlen(ptr[k]) > 1 && strlen(ptr1[i]) > 1)
    {
        while ((strlen(ptr[k]) > 1 && strlen(ptr1[i]) > 1) && isFound == 0)
        {
            totalCounter++;
            lineCounter++;
            if(strcmp(ptr[k], ptr1[i]) == 0)
            {
                printf("'%s' is word %d in the list\n", ptr[k], lineCounter);
                isFound = 1;
            }
            k++;
        }
        if (isFound == 0)
            {
                printf("'%s' is not in the word list\n", ptr1[i]);
            }
        isFound = 0;
        lineCounter = 0;
        k = 0;
        i++;
        wordCounter++;
    }

你 运行 超出了有效的数组索引

你不断递增 k 在:

while ((strlen(ptr[k]) > 1 && strlen(ptr1[i]) > 1) && isFound == 0)

在此处添加对 k 的检查,例如

while ((strlen(ptr[k]) > 1 && strlen(ptr1[i]) > 1) && isFound == 0 && k < k_max)

我在

中也一样
while (strlen(ptr[k]) > 1 && strlen(ptr1[i]) > 1)

在这里为 i 添加一个检查,例如:

while (strlen(ptr[k]) > 1 && strlen(ptr1[i]) > 1 && i < i_max)

我运行下面的代码通过C编译器。

#include <assert.h> //  Conditionally compiled macro that compares its argument to zero
#include <complex.h> // (since C99) Complex number arithmetic
#include <ctype.h> //   Functions to determine the type contained in character data
#include <errno.h> //   Macros reporting error conditions
#include <fenv.h> // (since C99)    Floating-point environment
#include <float.h> //   Limits of float types
#include <inttypes.h> // (since C99)    Format conversion of integer types
#include <iso646.h> // (since C95)  Alternative operator spellings
#include <limits.h> //  Sizes of basic types
#include <locale.h> //  Localization utilities
#include <math.h> //    Common mathematics functions
#include <setjmp.h> //  Nonlocal jumps
#include <signal.h> //  Signal handling
#include <stdalign.h> // (since C11)    alignas and alignof convenience macros
#include <stdarg.h> //  Variable arguments
#include <stdatomic.h> // (since C11)   Atomic types
#include <stdbool.h> // (since C99) Boolean type
#include <stddef.h> //  Common macro definitions
#include <stdint.h> // (since C99)  Fixed-width integer types
#include <stdio.h> //   Input/output
#include <stdlib.h> //  General utilities: memory management, program utilities, string conversions, random numbers
#include <stdnoreturn.h> // (since C11) noreturn convenience macros
#include <string.h> //  String handling
#include <tgmath.h> // (since C99)  Type-generic math (macros wrapping math.h and complex.h)
#include <time.h> //    Time/date utilities
#include <uchar.h> // (since C11)   UTF-16 and UTF-32 character utilities
#include <wchar.h> // (since C95)   Extended multibyte and wide character utilities
#include <wctype.h> // (since C95)
int main(){
  int ptr[]={0, 1, 2};
  int ptr1[]={3, 4, 5};
  int isFound=0;
  int wordCounter=0;
  int totalCounter=0;
  int lineCounter=0;
  int i=1;
  int k=0;
  while (strlen(ptr[k]) > 1 && strlen (ptr1[i]) > 1)
  {
  while ((strlen (ptr[k]) > 1 && strlen (ptr1[i]) > 1) && isFound == 0)
  {
    totalCounter++;
    lineCounter++;
    if (strcmp (ptr[k], ptr1[i]) == 0)
    {
      printf ("'%s' is word %d in the list\n", ptr[k], lineCounter);
      isFound = 1;
    }
    k++;
  }
  if (isFound == 0)
  {
    printf ("'%s' is not in the word list\n", ptr1[i]);
  }
  isFound = 0;
  lineCounter = 0;
  k = 0;
  i++;
  wordCounter++;
  }
}

这是我将其放入编译器时捕获的错误。

main.c:38:17: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast [-Wint-conversion]

/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘int’

main.c:38:40: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast [-Wint-conversion]

/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘int’

main.c:40:19: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast [-Wint-conversion]

/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘int’

main.c:40:42: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast [-Wint-conversion]

/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘int’

main.c:44:16: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]

/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘int’

segmentation fault

program finished with exit code 139

你为数组成员做了字符串长度,即 strlen(ptr[k]),这将尝试评估数组 p 位置 k 处可用成员的 len。所以最终处理将是

  1. strlen(ptr[0]); //由于k初始化为0, -> 然后它将被评估为
  2. strlen(0); //因为p是一个有元素{0,1,2}的数组,所以在第0个位置,0是元素 -> 所以,这里 strlen 将尝试评估整数类型的长度,这超出了 strlen
  3. 的范围

由于strlen是一个字符串函数,所以只能计算字符串。