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

From Helpful
Jump to: navigation, search
m
m (Are unused variables optimized away?)
Line 175: Line 175:
 
How it does so can also vary.
 
How it does so can also vary.
  
In the end, all that matters is that the result of the code is correct,  
+
In the end, all that matters is that the result of all uses is correct,  
so if you have complex code that the compiler can work out is essentially a constant, it will use that constant. [https://godbolt.org/z/UmDUxn  A stolen example] (from [https://stackoverflow.com/questions/55060820/does-an-unused-member-variable-take-up-memory/55061163#55061163])
+
so if you have complex code that the compiler can work out is essentially a constant (on the platform), it will use that constant, and may be able to propagate it to a single constant.  
 +
Consider [https://godbolt.org/z/UmDUxn  A stolen example] (from [https://stackoverflow.com/questions/55060820/does-an-unused-member-variable-take-up-memory/55061163#55061163]) where all addressing, copies, and adds are reduced to a single constant.
  
  
for context, remember that a compiler produces code. This means it's all about the functions.
+
For context, remember that a compiler produces code.
  
Data structures are not code, data structures are more like temporary implicit contracts about address offsets.
+
Data structures are not code, it's all about the functions.
 +
 
 +
Data structures are more like temporary implicit contracts about address offsets.
  
 
Assembly doesn't care about your groupings, all it needs is that addressing be ''consistent'' between all uses.
 
Assembly doesn't care about your groupings, all it needs is that addressing be ''consistent'' between all uses.
  
 +
And each use of a struct may be optimised independently.
  
  
'''For structs''' there are a few more rules,
 
  
 
+
'''For structs''' there are a few more rules,
 
+
in part because when these are passed to multiple functions,
But be aware that each use of a struct may be optimised independently.
+
the mental model is base-and-offset,
  
  
Line 221: Line 224:
  
 
'''But won't that break things like incremental linking and APIs?'''  
 
'''But won't that break things like incremental linking and APIs?'''  
 +
 +
No, for more complex reasons
 +
 +
 +
'''But won't that break things like debugging symbols?'''
  
  
  
 +
static data members versus per-instance members
  
  

Revision as of 12:47, 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 having to wrap them in #ifndef NDEBUG ... #endif.

If an assert fails:

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



Optimization notes

Are unused variables optimized away?