简体   繁体   English

使用char指针(C)的分段错误

[英]segmentation fault using char pointer (C)

I've been trying to figure out what I'm doing wrong in this block of code for a while now and still no luck. 我一直试图弄清楚我在这段代码中做错了什么,现在仍然没有运气。 According to GDB, I'm receiving a SIGSEV, Segmentation Fault which means that I tried to access an invalid memory address. 根据GDB,我收到了SIGSEV(分段错误),这意味着我试图访问无效的内存地址。

Here I'm trying to find the length of the char pointer that is being passed 在这里,我试图找到要传递的char指针的长度

int getLength(char *start) {
 int count = 0;
 while(*start) {
   count++;
   start++;
 }
 return count;
}

Here is my full C file. 这是我的完整C文件。 I think it might be useful 我认为这可能有用

  #include <stdlib.h>
  #include <stdio.h>

  int main(int argc, char *argv[]) {
    int len1 = getLength(argv[1]);
    int len2 = getLength(argv[2]);  

    if (argc != 3) {
     fprintf(stderr, "Invalid number of items input.\n");
     return 1;
    }

    if (len1!=len2) {
     fprintf(stderr,"From and to are not the same size.\n");
     return 1;
    }
    return 0;   
 }

 int getLength(char *start) {
    int count = 0;
    while(*start) {
     count++;
     start++;
    }
   return count;
 }

 int duplicatesFrom(char *from) {
   int i;
   int j;

   int len = getLength(from); 

   for(i=0;i<len;i++) {
    for(j=0;j<len;j++) {
      if (from[i]==from[j] && i!=j) {
        return 1;
      }
    }
   }
   return 0;
}

First, if you don't give at least 3 arguments to your program, it will crash since you call getLength with the second and third argument as parameters at the beginning of main() . 首先,如果您不给程序至少提供3个参数,则由于调用getLength并将main()的第二个和第三个参数作为参数,它会崩溃。

I suggest you move the two first lines below the argument count check. 我建议您将前两行移到参数计数检查下面。

Then, as suggested by BathSheba, you shoud consider making the char * passed to getLength() const since getLength() doesn't modify it. 然后,如BathSheba所建议,您应该考虑将char *传递给getLength() const因为getLength()不会对其进行修改。 It is a good practice and make the code more self-explanatory. 这是一个好习惯,可以使代码更容易说明。

Also, do not forget the forward declaration of the functions you use in main() if their definition are below main() , unless you put the forward declaration in a header file (neither are present in your code snippet). 另外,如果它们的定义在main()之下,也不要忘记在main()使用的函数的前向声明,除非您将前向声明放在头文件中(代码片段中都没有)。

On last thing : in getLength() , you should check if the char * passed is valid by doing something like if (start){...} . 最后一件事:在getLength() ,您应该通过执行if (start){...}类的操作来检查所传递的char *是否有效。 Because if start == NULL , your program will crash since you will try to access a null pointer. 因为如果start == NULL ,则您的程序将崩溃,因为您将尝试访问空指针。

Following your edit : you can not separate an if() statement and the else if() logically following it. 编辑之后:您不能在逻辑上将if()语句和else if()分开。 The else if() can be changed to if() here since in the case you enter the first if() , you quit the program ( return 1; ). 在这里else if()可以更改为if() ,因为在您输入第一个if()的情况下,您退出了程序( return 1; )。 And if you don't (if there is 3 arguments passed to the program), you can check safely if argv[1] and argv[2] have the same length. 如果不这样做(如果有3个参数传递给程序),则可以安全地检查argv[1]argv[2]的长度是否相同。

Your main, slightly modified : 您的主要,稍作修改:

int main(int argc, char *argv[]) {

    if (argc != 3) {
        fprintf(stderr, "Invalid number of items input.\n");
        return 1;
    }

    int len1 = getLength(argv[1]);
    int len2 = getLength(argv[2]);

    if (len1 != len2) {
        fprintf(stderr, "%s and %s are not the same size.\n", argv[1], argv[2]);
        return 1;
    }
    else {
        printf("%s", "Hello\n");
    }

    return 0;
}

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

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