constraint_handler_c
is a type for error handlers that is invoked when a run-time error occurs due to some bounds-checking function. When an error like this occurs, set_constraint_handler_s
is called. Some error handling function – which must be of type constraint_handler_c
– is then passed to set_constraint_handler_s
. If no function is explicitly passed, a null pointer may be passed or the default handler may be called.
The type of the handler must be of constraint_handler_t
, which is defined as:
typedef void (*constraint_handler_t)( const char *restrict message,
void *restrict ptr,
errno_t error);
#include<stdlib.h>
message
- Pointer to the message describing the error.
ptr
- Pointer to an object that provides the name of the function that detects the error. It can alternatively be NULL
.
error
- The error code encountered.
#include<stdio.h> #include<stdlib.h> void getDecision() { char response[8]; size_t length = sizeof(response); puts("Continue? [y] n : " ); if ((gets_s (response, length ) == NULL) || (response[0] == ‘n’)) { exit(0); } } int main() { constraint_handler_t temp = set_constraint_handler_s(ignore_handler_s); getDecision(); }
In this example, we add a call to set_constraint_handler_s
to install ignore_handler_s
as the runtime constraint handler. If the runtime constraint handler is set to ignore_handler_s
, any library function in which a runtime constraint violation occurs will return to its caller. The caller can then determine whether runtime constraint violation occurs based on the library function’s specifications.
RELATED TAGS
CONTRIBUTOR
View all Courses