名稱空間
變體
操作

abort_handler_s

來自 cppreference.com
< c‎ | 錯誤
在標頭檔案 <stdlib.h> 中定義
void abort_handler_s( const char * restrict msg,

                      void * restrict ptr,
                      errno_t error

                    );
(C11 起)

stderr 寫入一個實現定義的訊息,該訊息必須包含 msg 指向的字串,然後呼叫 abort()

可以將指向此函式的指標傳遞給 set_constraint_handler_s,以建立一個執行時約束違規處理程式。

與所有邊界檢查函式一樣,只有當實現定義了 __STDC_LIB_EXT1__ 且使用者在包含 <stdlib.h> 之前將 __STDC_WANT_LIB_EXT1__ 定義為整數常量 1 時,才保證 abort_handler_s 可用。

目錄

[編輯] 引數

msg - 指向寫入標準錯誤流的訊息的指標
ptr - 指向實現定義物件或空指標的指標。實現定義物件的示例是提供檢測到違規的函式名稱和檢測到違規的行號的物件
error - errno_t 型別的一個正值

[編輯] 返回值

無;此函式不會返回到其呼叫者

[編輯] 注意

如果從未呼叫 set_constraint_handler_s,則預設處理程式是實現定義的:它可能是 abort_handler_signore_handler_s 或其他實現定義的處理程式。

[編輯] 示例

#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 = 22
abort_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 result
of a bug present in the software. Please reach out to your
software's vendor to get more help.
Aborted

[編輯] 參考

  • C11 標準 (ISO/IEC 9899:2011)
  • K.3.6.1.2 abort_handler_s 函式 (p: 605)

[編輯] 另請參閱

邊界檢查函式的忽略回撥。
(函式) [編輯]
為邊界檢查函式設定錯誤回撥。
(函式) [編輯]