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

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

[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\r\n",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\r\n",strerror(errno));
  }

  pid = fork();
  if(pid<0)
  {
    exit(1);
  }
  if(pid==0)
  {
     printf("i 'm child\r\n");
     close(pipefd[1]);
     while(1)
     {
       sleep(200);
       ret = read(pipefd[0],buf,127);
       if(ret >0 )printf("child recv parent %s\r\n",buf);
     }
  }
  else
  {
    printf("i'm parent\r\n");
    close(pipefd[0]);
    while(1)
    {
    i++;
    sprintf(buf,"parent send msg %d\r\n",i);
    ret = write(pipefd[1],buf,strlen(buf));
  if(ret <0)
    {
       fprintf(stderr, "parent write pipe failed %s\r\n",strerror(errno));
    }
    }
   wait(NULL);
  }
}

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

转载于:https://www.cnblogs.com/10087622blog/p/10655706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值