当exit(0)用于退出程序时,不会调用本地作用域的非静态对象的析构函数.但是如果使用return 0则调用析构函数.注意即使我们调用exit()也会清理静态对象.
这种逻辑背后应该有一些原因.我只是想知道它是什么?谢谢.
我的老师让我写一个不执行任何系统调用的C函数.函数什么都不做就没有关系.
以下函数是否执行任何系统调用?
int func() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果确实如此,你能给我一个像我正在寻找的样本功能吗?
非常感谢.
我查看了链接退出和退货有什么区别? 并 在main()中返回语句vs exit() 来查找答案,但是徒劳无功.
第一个链接的问题是答案取决于return任何函数.我想知道在main()函数中两者之间的确切差异.即使有一点点差异,我也想知道它是什么.哪个是首选,为什么?在关闭各种编译器优化的情况下使用returnexit()(或exit()return)是否有任何性能提升?
第二个链接的问题是我对知道C++中发生的事情并不感兴趣.我想要特别关于C的答案.
编辑:经过一个人的推荐,我实际上试图比较以下程序的汇编输出:
注意:使用 gcc -S <myprogram>.c
程序mainf.c:
int main(void){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
装配输出:
.file "mainf.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.9.2-10ubuntu13) 4.9.2"
.section .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)
程序mainf1.c:
#include <stdlib.h>
int main(void){
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
装配输出:
.file "mainf1.c" …Run Code Online (Sandbox Code Playgroud) 我注意到FreeBSD代码/bin并且/usr/bin有一些修复使用exit而不是return,这是什么意思?
我所有的想法都是return声明可能导致vfork(2)损坏堆栈帧,这是唯一的原因吗?如果这是真的,那么为什么只是在命令部分/bin和/usr/bin得到修复,不是所有的人?
我刚刚阅读了Accelerated C++的第一章(看起来像一本很棒的书),最后作者说
但是,明确包括从主要回归是一种良好的做法
为什么这被认为是好习惯?在C99中,我总是省略return 0,exit()用于表示异常程序终止,并且从不错过显式返回.
所以我仍然习惯于模块化编程,并希望确保我坚持最佳实践.如果我有下面的两个模块头文件,是否会#included多次包含每个文件的标题(例如"mpi.h")?有没有正确的方法来解释这个?
此外,我的模块标题通常看起来像这些示例,因此任何其他批评/指针都会有所帮助.
/* foo.h */
#ifndef FOO_H
#define FOO_H
#include <stdlib.h>
#include "mpi.h"
void foo();
#endif
Run Code Online (Sandbox Code Playgroud)
和
/* bar.h */
#ifndef BAR_H
#define BAR_H
#include <stdlib.h>
#include "mpi.h"
void bar();
#endif
Run Code Online (Sandbox Code Playgroud)
并使用示例程序:
/* ExampleClient.c */
#include <stdlib.h>
#include <stdio.h>
#include "mpi.h"
#include "foo.h"
#include "bar.h"
void main(int argc, char *argv[]) {
foo();
MPI_Func();
bar();
exit(0)
}
Run Code Online (Sandbox Code Playgroud) 嗨,我想知道从主函数返回和退出之间的区别是什么。调用它们中的每一个时,幕后会发生什么,以及在每种情况下如何返回控件。如果有人可以深入研究这个主题,我真的会很高兴。
我想知道什么时候应该使用exit()函数覆盖return语句.我可以使用以下任一语句结束程序:
exit(0);
or
return;
Run Code Online (Sandbox Code Playgroud)
我应该使用哪一个?何时使用?使用有什么好处exit()吗?
我知道returnvsexit()之间的区别.
我想知道它们有什么不同FreeConsole().
FreeConsole() 是一个API函数
(scope: main function) return 是C中的陈述.
exit (EXIT_SUCCESS) 是一个函数调用.
您想在C中使用哪个退出程序?为什么?
我知道return和exit()(链接)之间的区别,但我不知道在何时何地选择一个而不是另一个.例如,根据这个答案,我理解这return是一个更好的选择,但从另一个我理解相反.
一个例子:在这个代码中(来自这个问题)是否优先使用exit()或return?
int read_file (char *filename, int **vet)
{
FILE *fin;
if ( !(fin = fopen(filename, "r")) )
{
perror(filename);
return -1;
}
* vet = malloc (10 * sizeof(int));
if ( *vet == NULL )
{
perror("Memory allocation error.\n");
return -2;
}
/* ... */
return fclose(fin);
}
int main ()
{
char filename[100];
int *vet;
if ( read_file(filename, &vet) ) …Run Code Online (Sandbox Code Playgroud) c ×9
c++ ×4
exit ×4
return ×4
assembly ×1
destructor ×1
freebsd ×1
function ×1
mingw32 ×1
return-value ×1
system-calls ×1
unix ×1
winapi ×1