程序崩溃,我不知道为什么

时间:2019-11-13 02:14:23

标签: c crash

程序崩溃,我不知道为什么。 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++;
    }

3 个答案:

答案 0 :(得分: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是一个字符串函数,因此只能评估字符串。

答案 1 :(得分:0)

您用尽了有效的数组索引

您不断增加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)

在此处为我添加支票,例如:

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

答案 2 :(得分:0)

我通过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