【问题标题】:Program crashes and I can't figure out why程序崩溃,我不知道为什么
【发布时间】:2019-11-13 02:14:23
【问题描述】:

程序崩溃了,我不知道为什么。 ptr 和 ptr1 是 char** 并且 ptr 和 ptr1 的每个元素都是一个 char*,如果已满,则不是所有的 malloc 空间,但是,每个元素都会留下一些空指针。我认为问题出在这个片段上,因为在崩溃之前打印的最后一件事是在这个片段中。

 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++;
    }

【问题讨论】:

  • 关于:(strlen(ptr[k])和类似的表达式:由于ptr是指向指针的指针,需要类似于( strlen(*ptr)[k] )的代码

标签: c crash


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您用完了有效的数组索引

    你不断增加 k in:

    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)
    

    【讨论】:

    • 程序本身崩溃,而不是由于无限循环
    • 假设 k 达到 41,而您只定义了内存到 40,ptr1[k] 将抛出分段错误并崩溃。
    【解决方案3】:

    我通过 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:警告:传递“strlen”的参数 1 会生成指针 从没有强制转换的整数 [-Wint-conversion]

    /usr/include/string.h:399:15:注意:预期为“const char *”,但 参数的类型是“int”

    main.c:38:40: 警告:传递‘strlen’的参数 1 会生成指针 从没有强制转换的整数 [-Wint-conversion]

    /usr/include/string.h:399:15:注意:预期为“const char *”,但 参数的类型是“int”

    main.c:40:19: 警告:传递“strlen”的参数 1 会生成指针 从没有强制转换的整数 [-Wint-conversion]

    /usr/include/string.h:399:15:注意:预期为“const char *”,但 参数的类型是“int”

    main.c:40:42: 警告:传递“strlen”的参数 1 生成指针 从没有强制转换的整数 [-Wint-conversion]

    /usr/include/string.h:399:15:注意:预期为“const char *”,但 参数的类型是“int”

    main.c:44:16: 警告:传递‘strcmp’的参数 1 会生成指针 从没有强制转换的整数 [-Wint-conversion]

    /usr/include/string.h:144:12:注意:预期为“const char *”,但 参数的类型是“int”

    分段错误

    程序以退出代码 139 结束

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多