简体   繁体   English

返回指针时出现分段错误

[英]Segmentation fault when returning a pointer

I'm trying to make a code in C that can get environment variable and then search for a specific word from that result using strstr. 我正在尝试在C中创建一个可以获取环境变量的代码,然后使用strstr从该结果中搜索特定的单词。 I'm using UBUNTU OS and gcc compiler. 我正在使用UBUNTU OS和gcc编译器。 Here is the code that I've written. 这是我写的代码。 The comment are what I expected to happen. 评论是我期望发生的。

#include <stdio.h>
#include <string.h>
#include <unistd.h>

extern char **environ;
extern char **tmp;
extern char **tmp2;

char *search_string(char *tmp,int x)
{
      char string[]="ABC";          //I'm looking for ABC in the environment variable
      char *pointer;
      pointer=strstr(tmp,string);   //pointer will point to the result of strstr
      if(pointer != NULL)
      {       printf("%s ,",tmp);
              printf("data found : %s \n",pointer);
      } else  {
              //hope to do something
      }
      return (pointer);
}

int main(char *tmp2)
{
      int x = 0;
      for(x=0;environ[x]!='\0';x++){   //I'm expecting it to keep looping until finish
      tmp2=search_string(environ[x],x); //tmp2 will point to the function return value
      printf("%s\n",tmp2);             //print the return value
      }  //If the search_string return NULL, does it consider string or something else?
      return 0;
}

After running the code, it crashes because of core dump. 运行代码后,由于核心转储而崩溃。 Here are the output. 这是输出。

ABC=/tmp ,data found : ABC=/tmp 
ABC=/tmp
Segmentation fault (core dumped)

From what I see, it can only do the search_string for only 1 time. 从我看来,它只能执行一次search_string。 Then it crashes. 然后它崩溃了。 Then I use gdb to find out at what line does it actually crash and here are the result: 然后我使用gdb找出它实际崩溃的行,这是结果:

Starting program: /home/fikrie/a.out 
ABC=/tmp ,data found : ABC=/tmp 
ABC=/tmp

Program received signal SIGSEGV, Segmentation fault.
__strlen_ia32 () at ../sysdeps/i386/i686/multiarch/../../i586/strlen.S:99
99  ../sysdeps/i386/i686/multiarch/../../i586/strlen.S: No such file or directory.

What I dont understand from the debug is that it's receiving error because of SEGV signal. 我从调试中得知的是,由于SEGV信号,它正在接收错误。 Can someone point me on how to solve this problem? 有人能指出我如何解决这个问题吗? Is it because the search_string returns a NULL value? 是因为search_string返回NULL值吗?

The problem is that if search_string() doesn't find the string, it returns NULL . 问题是如果search_string()没有找到字符串,它将返回NULL You then pass that NULL to printf() , which crashes. 然后将该NULL传递给printf() ,它会崩溃。

In main() , you need something like: main() ,您需要以下内容:

if (tmp2)
    printf("%s\n", tmp2);

Also, the tmp2 variable should be of type char * , not char ** . 此外, tmp2变量应为char *类型,而不是char ** And there's no reason not to declare it local to main() . 并且没有理由不将它声明为main()本地。

A very simple change to your main loop stops the program from crashing: 对主循环的一个非常简单的更改会阻止程序崩溃:

int main(char *tmp2)
{
      int x = 0;
      for(x=0;environ[x]!='\0';x++){   //I'm expecting it to keep looping until finish
      tmp2=search_string(environ[x],x); //tmp2 will point to the function return value
// >>>>> change these next two lines:
      if(tmp2 != NULL) printf("%s\n",tmp2);             //print the return value
      else printf("%s does not contain ABC\n", environ[x]);
// <<<<< end of change
      }  //If the search_string return NULL, does it consider string or something else?
      return 0;
}

Note that if you only expect one match, you could add a break; 请注意,如果您只想要一个匹配,则可以添加一个break; when you print the match. 当你打印比赛。 The above code prints out all the environment variables - you can see it doesn't stop... 上面的代码打印出所有的环境变量 - 你可以看到它不会停止......

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

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