?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
在頭文件<stdlib.h>中定義 | ||
---|---|---|
void abort_handler_s(const char * restrict msg,void * restrict ptr,errno_t error); | (自C11以來(lái)) |
寫(xiě)入一個(gè)實(shí)現(xiàn)定義的消息,stderr
其中必須包含指向的字符串msg
和調(diào)用abort()
。
可以將指向此函數(shù)的指針傳遞給set_constraint_handler_s以建立運(yùn)行時(shí)約束違規(guī)處理程序。與所有邊界檢查的函數(shù)一樣,abort_handler_s
只有__STDC_LIB_EXT1__
在實(shí)現(xiàn)定義并且用戶在包含之前定義__STDC_WANT_LIB_EXT1__
為整數(shù)常量時(shí)1
才能保證可用<stdlib.h>
。
msg | - | 指向?qū)懭霕?biāo)準(zhǔn)錯(cuò)誤流的消息的指針 |
---|---|---|
ptr | - | 指向?qū)崿F(xiàn)定義的對(duì)象或空指針的指針。實(shí)現(xiàn)定義對(duì)象的示例是對(duì)象,該對(duì)象給出檢測(cè)到違規(guī)的函數(shù)的名稱以及檢測(cè)到違規(guī)時(shí)的行號(hào) |
error | - | errno_t類型的正值 |
沒(méi)有; 這個(gè)函數(shù)不會(huì)返回給調(diào)用者。
如果set_constraint_handler_s
是從來(lái)不叫,默認(rèn)的處理程序是實(shí)現(xiàn)定義的:它可能是abort_handler_s
,ignore_handler_s
或其他一些實(shí)現(xiàn)定義的處理程序。
#define __STDC_WANT_LIB_EXT1__ 1#include <string.h>#include <stdio.h>#include <stdlib.h> int main(void){#ifdef __STDC_LIB_EXT1__ char dst[2]; set_constraint_handler_s(ignore_handler_s); int r = strcpy_s(dst, sizeof dst, "Too long!"); printf("dst = \"%s\", r = %d\n", dst, r); set_constraint_handler_s(abort_handler_s); r = strcpy_s(dst, sizeof dst, "Too long!"); printf("dst = \"%s\", r = %d\n", dst, r);#endif}
可能的輸出:
dst = "", r = 22abort_handler_s was called in response to a runtime-constraint violation. The runtime-constraint violation was caused by the following expression in strcpy_s:(s1max <= (s2_len=strnlen_s(s2, s1max)) ) (in string_s.c:62) Note to end users: This program was terminated as a resultof a bug present in the software. Please reach out to your software's vendor to get more help.Aborted
C11標(biāo)準(zhǔn)(ISO/IEC 9899:2011):
K.3.6.1.2 abort_handler_s函數(shù)(p:605)
ignore_handler_s(C11) | 忽略邊界檢查函數(shù)的回調(diào)(函數(shù)) |
---|---|
set_constraint_handler_s(C11) | 為邊界檢查函數(shù)(函數(shù))設(shè)置錯(cuò)誤回調(diào) |