C++中,exit和return有什么不同?
2012-01-10 13:06
387 查看
总能看到,但是你知道这两者有什么不同呢?
------------------------------------------------------------------------
exit() is used to exit the program as a whole. In other words it returns control to
the operating system.Afterexit() all memory and temporary storage areas are all flushed
out and control goes out of program.
In contrast, the return() statement is used to return from a function
and return control to the calling function.
Also in a program there can be only one
exit
() statement but a function can have number of return statements. In other words there is no restriction on the number of return statements that can be present in a function.
------------------------------------------------------------------------
不知道这个不同会不会有问题?通常会,除非你不用他们。
最近review一个老代码的CR时,碰到了一个跟它相关的问题。
void main()
{
CRuntimeEnvInit a;
//a bunch of logic here...
exit(l_exitCode);
return 0;
}
在vs2010上,这会有什么问题?a的析构函数不会被调用。这似乎不要紧,如果有内存泄漏,这个时候也会被OS收回。但要命的是CRuntimeEnvInit 会初始化很多全局的东西,包括打开重要文件。这样一来,这些文件总是被打开!!!
为什么会这样?通过“go to disassembly”可以得知,析构函数时在return那里,exit直接退出,导致了析构函数没有被调用。
所以结论是什么呢?
When I call returnin
destructor will be called for my locally scoped objects! Re-read that.
does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files (surely you want all
your data flushed to disk?).
Note that
选用这两者之前,要想清楚。
还有一种方案,针对全局变量和main函数的local变量, 那就是register一个函数给atexit,并在这个函数中写上release resource的代码。然后不管何时exit,总能把resource release。
参考
http://groups.google.com/group/gnu.gcc.help/msg/8348c50030cfd15a http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main
------------------------------------------------------------------------
exit() is used to exit the program as a whole. In other words it returns control to
the operating system.Afterexit() all memory and temporary storage areas are all flushed
out and control goes out of program.
In contrast, the return() statement is used to return from a function
and return control to the calling function.
Also in a program there can be only one
exit
() statement but a function can have number of return statements. In other words there is no restriction on the number of return statements that can be present in a function.
------------------------------------------------------------------------
不知道这个不同会不会有问题?通常会,除非你不用他们。
最近review一个老代码的CR时,碰到了一个跟它相关的问题。
void main()
{
CRuntimeEnvInit a;
//a bunch of logic here...
exit(l_exitCode);
return 0;
}
在vs2010上,这会有什么问题?a的析构函数不会被调用。这似乎不要紧,如果有内存泄漏,这个时候也会被OS收回。但要命的是CRuntimeEnvInit 会初始化很多全局的东西,包括打开重要文件。这样一来,这些文件总是被打开!!!
为什么会这样?通过“go to disassembly”可以得知,析构函数时在return那里,exit直接退出,导致了析构函数没有被调用。
所以结论是什么呢?
When I call returnin
main(), destructors will be called for my locally scoped objects. If I callexit(),no
destructor will be called for my locally scoped objects! Re-read that.
exit()
does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files (surely you want all
your data flushed to disk?).
Note that
staticobjects will be cleaned up even if you call
exit(). Finally note, that if you use
abort(), no objects will be destroyed. That is, no global objects, no static objects and no local objects will have their destructors called.
选用这两者之前,要想清楚。
还有一种方案,针对全局变量和main函数的local变量, 那就是register一个函数给atexit,并在这个函数中写上release resource的代码。然后不管何时exit,总能把resource release。
参考
http://groups.google.com/group/gnu.gcc.help/msg/8348c50030cfd15a http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main
相关文章推荐
- error C2381: “exit” : 重定义;__declspec(noreturn) 不同
- C# 与 C/C++ 有什么不同 之 数据类型和变量
- C#, Java 和 c++的特点,有什么相同的地方?不同的地方?C#分别从c++和java中吸取了他们那些优点?
- 挑战30天C++入门极限-exit()与return()函数的差别
- c++中箭头操作符(->)和点操作符(.)有什么不同
- C++和Java有什么不同
- c++中箭头操作符(->)和点操作符(.)有什么不同
- C语言中return、exit(0)与exit(1)有什么区别
- error C2381 “exit” 重定义;__declspec(noreturn) 不同
- 函数的返回类型可以为值、指针、引用,这三种返回类型有什么不同,各有什么好处?(c++)
- 什么是无符号段整数,什么又是有符号数,(c++与java语言里边的不同)
- Linux编程中的坑——C++中exit和return的区别
- C++里if语句的return 什么作用
- c++中引用和指针有什么不同?指针加上什么限制等于引用?
- C++技术问题总结-第13篇 调用约定有哪些,有什么不同
- error C2381: “exit” : 重定义;__declspec(noreturn) 不同
- error C2381: “exit” : 重定义;__declspec(noreturn) 不同
- 结束程序的代码return 0 exit(0) ExitProcess(0)的不同之处
- return x与exit(x)到底有什么区别
- c++中箭头操作符(->)和点操作符(.)有什么不同