set_constraint_handler_s
stdlib.h:インデックス への移動
ヘッダー ファイル
stdlib.h
カテゴリ
実行時制約処理
プロトタイプ
constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
説明
handler の値を実行時制約ハンドラとして設定します。 実行時制約ハンドラは、実行時制約エラーが発生したときに呼び出される関数です。 ハンドラを登録すると、前のハンドラと置き換わります。つまり、新たに登録されたハンドラのみ呼び出されることになります。 ユーザー定義でないハンドラが既に設定されている場合や、ユーザーがハンドラを NULL ポインタに設定しようとする場合は、デフォルト ハンドラが使用されます。
新しい制約ハンドラとして指定される関数は、以下のような定義の constraint_handler_t 型でなければなりません。
typedef void (*constraint_handler_t)(const char * restrict msg, void * restrict ptr, errno_t error);
この関数が呼び出される際に指定される引数の意味は以下のとおりです。
名前 | 意味 |
---|---|
msg |
エラーについての情報が格納された char*(文字列)へのポインタです。 |
ptr |
オブジェクトへのポインタまたは NULL ポインタです。 |
error |
エラーが発生した関数の戻り値型が error_t 型の場合は、その戻り値が返されます。 それ以外の場合は、負でない不定値が格納されます。 |
戻り値
正常に終了した場合、set_constraint_handler_s は、前に割り当てられていた制約ハンドラへのポインタを返します。 ファイル終端(EOF)に達した場合やエラーの場合には、NULL を返します。
例
#include <string.h> #include <stdio.h> #include <stdlib.h> void myRuntimeConstraintHandler(const char* msg, void * ptr, errno_t error){ printf_s("A runtime constraint violation has occurred in function: %s and the function exited with error number: %d", msg, error); } int main(void) { char* string = NULL; set_constraint_handler_s(myRuntimeConstraintHandler); gets_s(string, 5); return 0; }