与终止程序相关的函数

exit

 

Performs complete C library termination procedures, terminates the process, and exits with the supplied status code.

 

void exit( int status );

 

Required Header <stdlib.h>

Parameter  status   Exit status

Remarks

The exit terminates the calling process. exit calls, in last-in-first-out (LIFO) order, the functions registered by atexit and _onexit, then flushes all file buffers before terminating the process.The status value is typically set to 0 to indicate a normal exit and set to some other value to indicate an error.

Although the exit and _exit calls do not return a value, the low-order byte of status is made available to the waiting calling process, if one the waiting calling processexists, after the calling process exits. The status value is available to the operating-system batch command ERRORLEVEL and is represented by one of two constants: EXIT_SUCCESS, which represents a value of 0, or EXIT_FAILURE, which represents a value of 1.

 

atexit

Processes the specified function at exit.

int atexit( void ( __cdecl *func )( void ) );

Return Value

atexit returns 0 if successful, or a nonzero value if an error occurs.

Parameter

func Function to be called

Remarks

The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to atexit cannot take parameters. atexit and _onexit use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory.

Example

/* ATEXIT.C: This program pushes four functions onto

 * the stack of functions to be executed when atexit

 * is called. When the program exits, these programs

 * are executed on a "last in, first out" basis.

 */

 

#include <stdlib.h>

#include <stdio.h>

 

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

 

void main( void ){

   atexit( fn1 );

   atexit( fn2 );

   atexit( fn3 );

   atexit( fn4 );

   printf( "This is executed first./n" );

}

 

void fn1(){

   printf( "next./n" );

}

 

void fn2(){

   printf( "executed " );

}

 

void fn3(){

   printf( "is " );

}

 

void fn4(){

   printf( "This " );

}

 

Output

This is executed first.

This is executed next.

 

abort

Aborts the current process and returns an error code.

void abort( void );

Return Value

abort does not return control to the calling process. By default, it terminates the current process and returns an exit code of 3.

Remarks

The abort routine prints the message “abnormal program termination” and then calls raise(SIGABRT). The action taken in response to the SIGABRT signal depends on what action has been defined for that signal in a prior call to the signal function. The default SIGABRT action is for the calling process to terminate with exit code 3, returning control to the calling process or operating system. abort does not flush stream buffers or do atexit/_onexit processing.

abort determines the destination of the message based on the type of application that called the routine. Console applications always receive the message via stderr. In a single or multithreaded Windows application, abort calls the Windows MessageBox API to create a message box to display the message along with an OK button. When the user selects OK, the program aborts immediately.

When the application is linked with a debug version of the run-time libraries, abort creates a message box with three buttons: Abort, Retry, and Ignore. If the user selects Abort, the program aborts immediately. If the user selects Retry, the debugger is called and the user can debug the program if Just-In-Time (JIT) debugging is enabled. If the user selects Ignore, abort continues with its normal execution: creating the Example

/* ABORT.C:  This program tries to open a

 * file and aborts if the attempt fails.

 */

 

#include  <stdio.h>

#include  <stdlib.h>

 

void main( void ){

   FILE *stream;

 

   if( (stream = fopen( "NOSUCHF.ILE", "r" )) == NULL ){

      perror( "Couldn't open file" );

      abort();

   }

   else

      fclose( stream );

}

 

Output

Couldn't open file: No such file or directory

 

abnormal program termination

 

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
### C++ 中用于终止程序函数 在 C++ 编程语言中,有多种方法可以用来终止程序。以下是几种常见的函数及其用途: #### `std::terminate` 函数程序遇到无法处理的异常情况时,C++ 运行时环境会调用 `std::terminate()` 函数[^1]。此函数通常由编译器自动触发,例如在未捕获的异常被抛出或者在析构函数中抛出第二个异常的情况下。一旦进入 `std::terminate()`,默认的行为是直接终止整个程序。 ```cpp #include <iostream> #include <stdexcept> void may_throw(bool shouldThrow) { if (shouldThrow) { throw std::runtime_error("Error occurred"); } } int main() { try { may_throw(true); } catch (...) { std::cerr << "Caught an exception." << std::endl; // If no handler is available, std::terminate will be called. } std::cout << "Program continues..." << std::endl; return 0; } ``` 如果上述代码中的异常没有被捕获,则会触发 `std::terminate()` 的调用。 --- #### `_Noreturn` 关键字自定义终止函数 从 C++20 开始,程序员可以使用 `_Noreturn` 关键字声明某些特殊类型的函数,这些函数不会正常返回控制权给调用者。典型例子包括通过标准库函数如 `std::exit(int)` 或 `std::abort()` 来立即停止程序运行[^2]。 下面是一个简单的示例展示如何创建一个带有消息输出并退出的应用程序终止函数: ```cpp #include <cstdlib> // For EXIT_FAILURE and std::exit #include <iostream> _Noreturn void terminate_with_error(const std::string& message) { std::cerr << "Fatal error: " << message << '\n'; std::exit(EXIT_FAILURE); // Program terminates here with a failure status code. } int main() { bool success = false; if (!success) { terminate_with_error("Operation failed."); } std::cout << "This line won't execute.\n"; return 0; } ``` 在这个案例里,只要条件不满足就会调用 `terminate_with_error` 方法从而结束进程。 --- #### 协程支持下的新可能性(C++20) 随着 C++20 对协程特性的引入[^3],虽然主要目的是为了改善异步编程模型以及提供更灵活的任务管理方式,但在特定场景下也可以利用它来设计更为复杂的控制流结构甚至实现某种形式上的“软中断”。不过需要注意的是,目前大多数情况下仍然推荐采用传统的同步风格编写核心逻辑部分除非确实存在性能瓶颈或者其他硬需求迫使我们转向异步解决方案。 尽管如此,对于希望探索现代技术趋势的人来说了解这部分内容仍然是很有价值的。 --- #### 总结 综上所述,C++ 提供了多个层次的方式来安全有效地关闭应用程序,具体取决于实际应用场景和个人偏好选择合适的工具非常重要。无论是依赖内置机制还是构建定制化方案都能达到预期效果只是复杂度有所不同罢了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值