Difference between revisions of "C and C++ notes / Semi-sorted"

From Helpful
Jump to: navigation, search
m (Are unused variables optimized away?)
m (assert.h)
(One intermediate revision by the same user not shown)
Line 13: Line 13:
 
==Debugging==
 
==Debugging==
  
===assert.h ====  
+
===assert.h ===  
 
(ANSI-C, C99) {{verify}}
 
(ANSI-C, C99) {{verify}}
  
 
assert() statements (via preprocessor) that are only compiled in when NDEBUG is not defined.  
 
assert() statements (via preprocessor) that are only compiled in when NDEBUG is not defined.  
This lets you have debug expressions that are automatically omitted from production - without having to wrap them in #ifndef NDEBUG ... #endif.
+
 
 +
This lets you have debug expressions that are automatically omitted from production,
 +
without explicitly wrapping them in #ifndef NDEBUG ... #endif.
  
 
If an assert fails:  
 
If an assert fails:  
* error information is written stderr {{comment|(including __FILE__, __LINE__, __func__ if C99, and the assert expression as text)}}
+
* error information is written to stderr {{comment|(including __FILE__, __LINE__, __func__ if C99, and the assert expression as text)}}
 
* abort() is called
 
* abort() is called
  

Revision as of 11:48, 20 May 2020

Notes related to C and C++

Note: Some notes describe behaviour common to most variants - for C mostly meaning C89 (and a bit of C99), unless mentioned otherwise.


Preprocessor

Debugging

assert.h

(ANSI-C, C99) (verify)

assert() statements (via preprocessor) that are only compiled in when NDEBUG is not defined.

This lets you have debug expressions that are automatically omitted from production, without explicitly wrapping them in #ifndef NDEBUG ... #endif.

If an assert fails:

  • error information is written to stderr (including __FILE__, __LINE__, __func__ if C99, and the assert expression as text)
  • abort() is called



Optimization notes

Are unused variables optimized away?