Does anyone have a resource for C++ memory optimization guidelines? Best practices, tuning, etc?
That depends a lot on both your compiler and target environment (RISC, Unix/Linux, Windows). Most compilers will have such information.
There are utilities out there that enable you to trace memory leaks so that you can fix them during test. If you are going to dynamically allocate a lot of things (which usually is the case with C/C++), try to make sure you deallocate everything before destroying an object. To do this:
- If you value memory over processor, use smart pointers.
- If your class has any member variables that are pointers, make sure your destructor frees each one of the. Group your member variables together on your source code so that it's easy to compare those variables with the destructor.
- Avoid dynamic memory allocation whenever possible to avoid leaks. Prefer
std::string
over dynamic allocated char*
, etc.
Would there be ANY benefit on the
compiler or memory allocation to get
rid of protected and private since
there there are no items that are
protected and private in this class?
No, if I'm not mistaken, protected/private are only checked during compilation, so they don't affect performance even if there were items under the keywords.
Furthermore, it's important to understand that the compiler is very inteligent (usually more than the programmer) so it will optimized away anything it can; For example, let's you declare a variable, int a
, inside your constructor. And let's say you don't use it at all, you just forgot it there. Most of the compilers won't even save stack space to those variables. Others will need the user to activate Optimization so that this happens, but as a rule-of-thumb, your production version of any program should be compiled with optimization enabled, even if not on full.
About the update, that thing you looked at are pre-processors directives and are being used to do what is called selective compilation. Take a look here.