免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10195 | 回复: 59
打印 上一主题 下一主题

exit()和return 有什么区别? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-05 11:05 |只看该作者 |倒序浏览
小弟在UNIX下运行下面的程序时出现了Segmentation fault,不知道是什么原因
当把最后的return 0改为exit(0)后又正常了.哪位大侠能给解释下吗?

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/**********  open()  ***********/
#include <unistd.h>
/**********  read() and write()  ***********/
#include <string.h>
/**********  memset()  ***********/
#include <errno.h>
#include <stdio.h>



extern int erron;

int main()
{
        int fd;
        char buf[1024];
        int iSize;

        memset(buf,0x00,1204);

        /*打开一个文件*/
        if((fd = open("test.txt",O_RDWR)) == -1)
        {
                printf("Open Error\nError No=%d\n",errno);
                exit(1);
        }
        else  printf("Open File Success\n");

        /*读文件*/
        if((iSize = read(fd,buf,1024)) == -1)
        {
                printf("Read Error\nError No=%\n",errno);
                exit(1);
        }
        else
        {
                buf[iSize] = 0;
                printf("Read Success\nbuf=%s\n",buf);
                memset(buf,0x00,1024);
        }

        /*写文件*/
        strcpy(buf,"http://www.starit.com");
        if((iSize = write(fd,buf,strlen(buf))) == -1)
        {
                printf("Write Error\nError No=%d\n",errno);
                exit(1);
        }
        else
        {
                printf("Write Success\nThe iSize=%d\n",iSize);
                memset(buf,0x00,1024);
        }

        /*关闭文件*/
        if(close(fd) != 0)
        {
                printf("Close File Error\n");
                exit(1);
        }
        else
        {
                printf("Close File Success\n");
        }

        printf("111111111111111111\n");

        //exit(0);
        return 0;

论坛徽章:
0
2 [报告]
发表于 2006-12-05 11:08 |只看该作者
不清楚,exit不行可能是因为函数要返回值你没有用return返回的原因吧。

论坛徽章:
0
3 [报告]
发表于 2006-12-05 11:20 |只看该作者
我是说程序最后用return 0时出现问题(Segmentation fault),改为exit(0)后正常了

论坛徽章:
0
4 [报告]
发表于 2006-12-05 11:30 |只看该作者
char buf[1024];
        int iSize;

        memset(buf,0x00,1204);

论坛徽章:
0
5 [报告]
发表于 2006-12-05 12:55 |只看该作者
exit(0) 等同于 return 0

论坛徽章:
0
6 [报告]
发表于 2006-12-05 13:07 |只看该作者
原帖由 mik 于 2006-12-5 12:55 发表
exit(0) 等同于 return 0


反对,return 0相当于exit在main之外调用,
exit(0) 相当于即刻退出程序.
exit(0)不析构main函数中的局部对象.



  1. __some_entry ()
  2. {
  3.      ....
  4.      int rt = main (__argc, __argv);
  5.      ....
  6.      exit (rt);
  7. }
复制代码

论坛徽章:
0
7 [报告]
发表于 2006-12-05 13:14 |只看该作者
原帖由 飞灰橙 于 2006-12-4 21:07 发表


反对,return 0相当于exit在main之外调用,
exit(0) 相当于即刻退出程序.
exit(0)不析构main函数中的局部对象.



  1. __some_entry ()
  2. {
  3.      ....
  4.      int rt = main (__argc, __argv);
  5.     ...
复制代码


main函数的return等同于exit,没有你说的这些区别。

论坛徽章:
0
8 [报告]
发表于 2006-12-05 13:20 |只看该作者
原帖由 emacsnw 于 2006-12-5 13:14 发表


main函数的return等同于exit,没有你说的这些区别。


是吗?你真牛!
那么是谁调用main函数的呢?
main return后到哪里,exit后又到哪里呢?
main中的局部对象的析构问题呢?

论坛徽章:
0
9 [报告]
发表于 2006-12-05 13:29 |只看该作者
原帖由 飞灰橙 于 2006-12-5 13:07 发表


反对,return 0相当于exit在main之外调用,
exit(0) 相当于即刻退出程序.
exit(0)不析构main函数中的局部对象.


[code]
__some_entry ()
{
     ....
     int rt = main (__argc, __argv);
    ...



你应该好好地研读 UNIX 编程圣经 APUE 再说

论坛徽章:
0
10 [报告]
发表于 2006-12-05 13:32 |只看该作者
原帖由 mik 于 2006-12-5 13:29 发表



你应该好好地研读 UNIX 编程圣经 APUE 再说


嗯,回头用功的说。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP