3 ms·
That's the code, not what moonshinefe was talking about, and we know that most programmers rarely look at the source for standard library code unless they're de
by acdha 11y ago
That's the code, not what moonshinefe was talking about, and we know that most programmers rarely look at the source for standard library code unless they're debugging something.
Here's what the assert man page says on OS X:
The assert() macro tests the given expression and if it is false, the calling process is terminated. A
diagnostic message is written to stderr and the abort(3) function is called, effectively terminating
the program.
If expression is true, the assert() macro does nothing.
The assert() macro may be removed at compile time with the cc(1) option -DNDEBUG.
Note that the “may be removed” warning is at the end, where it's easy to miss and it mentions a specific compiler flag rather than the concept of a release build.
On Linux, the man page leads with that discussion:
If the macro NDEBUG was defined at the moment <assert.h> was last included, the macro assert() generates no
code, and hence does nothing at all. Otherwise, the macro assert() prints an error message to standard error
and terminates the program by calling abort(3) if expression is false (i.e., compares equal to zero).
The purpose of this macro is to help the programmer find bugs in his program. The message "assertion failed
in file foo.c, function do_bar(), line 1287" is of no help at all to a user.
You're not wrong but you're being very optimistic to assume that a) every C programmer, including those grinding away on massive projects, has learned about this and knows every compiler flag in use and preprocessor macro defined in every file and b) large applications haven't accreted so much code in assert calls in that the current maintainers are afraid to turn it off. Stories like this one pop up every so often in the programming community for a reason.