Mistake on this page?
Report an issue in GitHub or email us

Assert

Mbed OS provides a set of macros that evaluates an expression and prints an error message if the expression evaluates to false. There are two types of macros, one for evaluating the expression during runtime and one for compile-time evaluation. mbed_assert.h defines these macros.

Assert macros reference

Assert example

You can use the MBED_ASSERT macro for runtime evaluation of expressions. If the evaluation fails, an error message is printed to STDIO in the below format.

mbed assertation failed: <EVALUATED EXPRESSION>, file: <FILE NAME>, line <LINE NUMBER IN FILE>

Note that the MBED_ASSERT macro is available in the debug and develop build profiles but not in the release build profile.

The below function uses MBED_ASSERT to validate a pointer to serial_t object.

uint8_t serial_tx_active(serial_t *obj) {
    MBED_ASSERT(obj);
    ...
}

You can use the MBED_STATIC_ASSERT macro for compile-time evaluation of expressions. If the evaluation fails, the passed in error message prints, and the compilation fails.

The below function uses MBED_STATIC_ASSERT to validate the size of the equeue_timer structure.

void equeue_tick_init() {
    MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(Timer),"The equeue_timer buffer must fit the class Timer");
    ...
}
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.