Main Content

Run-Time Error Detection and Reporting in Standalone C/C++ Code

You can generate standalone libraries and executables that detect and report run-time errors, such as out-of-bounds array indexing. If the generated code detects an error, it reports the error and terminates the program.

During development, before you generate C/C++ code, it is a best practice to test the generated code by running the MEX version of your algorithm. However, some errors occur only on the target hardware. To detect these errors, generate the standalone C/C++ code with run-time error detection enabled. Run-time error detection can affect the performance of the generated code. If performance is a consideration for your application, do not generate production code with run-time error detection enabled.

By default, run-time error detection is disabled for standalone libraries and executables. To enable run-time error detection and reporting for standalone libraries and executables:

  • At the command line, use the code configuration property RuntimeChecks.

    cfg = coder.config('lib'); % or 'dll' or 'exe'
    cfg.RuntimeChecks = true;
    codegen -config cfg myfunction
  • In the MATLAB® Coder™ app, in the project settings dialog box, on the Debugging pane, select the Generate run-time error checks check box.

Run-time error detection and reporting in standalone code has these requirements and limitations:

  • The error reporting software uses fprintf to write error messages to stderr. It uses abort to terminate the application. If fprintf and abort are not available, you must provide them. The abort function abruptly terminates the program. If your system supports signals, you can catch the abort signal (SIGABRT) so that you can control the program termination.

  • Error messages are in English only.

  • Some error checks require double-precision support. Therefore, the hardware on which the generated code runs must support double-precision operations.

  • If the program terminates, the error detection and reporting software does not display the run-time stack. To inspect the stack, attach a debugger. Also, the error detection and reporting software does not release resources, such as allocated memory.

  • If the program terminates, the error detection and reporting software does not release resources, such as allocated memory.

  • In standalone code, the function error displays a message that indicates that an error occurred. To see the actual message specified by error, you must generate and run a MEX function.

  • In standalone code, if called with more than 1 argument, the function assert does not report an error and does not terminate execution. If called with a single argument, for example, assert(cond), if cond is not a constant true value, reports an error and terminates execution.

Related Examples

More About