zoukankan      html  css  js  c++  java
  • 你知道这段代码为什么会段错误么?

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

    [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。

  • 相关阅读:
    c#正则表达式应用实例
    C# 中堆栈,堆,值类型,引用类型的理解 (摘抄)
    c#用正则表达式获得指定开始和结束字符串中间的一段文本
    asp.net c#截取指定字符串函数
    <收藏>提高Web性能的14条法则(详细版)
    利用Anthem.net 实现前台javascript调用服务器端c#函数 及流程分析
    Anthem.net
    jQuery animate(滑块滑动)
    .NET使用母版页后,控件名称自动生成导致js无法正常操作.net控件的问题
    Cocos2dx跨平台Android环境配置
  • 原文地址:https://www.cnblogs.com/10087622blog/p/10655706.html
Copyright © 2011-2022 走看看