• 你知道这段代码为什么会段错误么?


    最近遇到一个很小的编译问题

    [root@localhost linux-inject-master]# gcc -g -o pipe.o pipe.c
    pipe.c: In function 'main':
    pipe.c:46: warning: incompatible implicit declaration of built-in function 'strlen'

    有编译warning的,会出段错误。

    [root@localhost linux-inject-master]# gcc -g -o pipe.o pipe.c
    pipe.c: In function 'main':
    pipe.c:46: warning: incompatible implicit declaration of built-in function 'strlen'
    [root@localhost linux-inject-master]# gdb ./pipe.o
    GNU gdb (GDB) Red Hat Enterprise Linux (7.2-50.el6)
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-redhat-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/caq/linux-inject-master/pipe.o...done.
    (gdb) r
    Starting program: /home/caq/linux-inject-master/pipe.o
    Detaching after fork from child process 19969.
    i 'm child
    i'm parent
    ^C
    Program received signal SIGINT, Interrupt.
    0x00000036216db560 in __write_nocancel () from /lib64/libc.so.6
    Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.7.x86_64
    (gdb) n
    Single stepping until exit from function __write_nocancel,
    which has no line number information.
    
    Program received signal SIGPIPE, Broken pipe.
    0x00000036216db560 in __write_nocancel () from /lib64/libc.so.6
    (gdb)
    Single stepping until exit from function __write_nocancel,
    which has no line number information.
    0x00000036216db599 in write () from /lib64/libc.so.6
    (gdb)
    Single stepping until exit from function write,
    which has no line number information.
    main (argc=1, argv=0x7fffffffe428) at pipe.c:47
    47          if(ret <0)
    (gdb)
    49             fprintf(stderr, "parent write pipe failed %s
    ",strerror(errno));
    (gdb)
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000003621647e2c in vfprintf () from /lib64/libc.so.6

    去掉这个编译warning,则不会。

    代码如下:

    #include <stdio.h>
    #include <errno.h>
    //#include <string.h>----------------注释掉这行,则会产生编译warning,也会段错误,
    #include <unistd.h>
    #include <stdlib.h>
    
    
    int main(int argc,char *argv[])
    {
      int pipefd[2];
      int ret=0;
      int pid=0;
      char buf[128]={0};
      int i =0;
      ret = pipe( pipefd);
    
      if(ret != 0)
      {
        printf("create pipe err=%s
    ",strerror(errno));
      }
    
      pid = fork();
      if(pid<0)
      {
        exit(1);
      }
      if(pid==0)
      {
         printf("i 'm child
    ");
         close(pipefd[1]);
         while(1)
         {
           sleep(200);
           ret = read(pipefd[0],buf,127);
           if(ret >0 )printf("child recv parent %s
    ",buf);
         }
      }
      else
      {
        printf("i'm parent
    ");
        close(pipefd[0]);
        while(1)
        {
        i++;
        sprintf(buf,"parent send msg %d
    ",i);
        ret = write(pipefd[1],buf,strlen(buf));
      if(ret <0)
        {
           fprintf(stderr, "parent write pipe failed %s
    ",strerror(errno));
        }
        }
       wait(NULL);
      }
    }

    段错误的原因,就在于strlen。

  • 相关阅读:
    学习python第二天数据库day1
    学习python第一天总纲
    Mac 键盘快捷键
    报错 Filtered offsite request
    mysql 顺序问题
    implode 把数组 组成一个字符串
    TP view中跳转到某个控制器
    生成商品的唯一货号
    数组排序
    判断文件是否存在 删除文件
  • 原文地址:https://www.cnblogs.com/10087622blog/p/10655706.html
Copyright © 2020-2023  润新知