set_constraint_handler_s
Go Up to stdlib.h Index
Header File
stdlib.h
Category
Runtime-constraint handling
Prototype
constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
Description
Sets the run-time constraint handler to the value of handler. A run-time constraint handler is a function that is called when a run-time constraint error occurs. Registering a handler replaces the former one, meaning that only the newly registered one will be called. A default handler is used if a handler that is not user-defined has been set or if the user tries to set it to a null pointer.
The function being given as the new constraint handler must be of type constraint_handler_t, which has the following definition:
typedef void (*constraint_handler_t)(const char * restrict msg, void * restrict ptr, errno_t error);
When this function is called, the arguments given to it have the following meanings:
Name | Meaning |
---|---|
msg |
Points to a char* that holds information about the error. |
ptr |
Points to an object or is null. |
error |
If the function where the error occurred is of return type error_t, then the return value is sent. Otherwise, it holds an indeterminable nonnegative value. |
Return Value
On success, set_constraint_handler_s returns the pointer to the previously assigned constraint handler. On end-of-file or error, it returns NULL.
Example
#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; }