Does anyone have a resource for C++ memory optimization guidelines? Best practices, tuning, etc?
As an example:
Class xxx {
public:
xxx();
virtual ~xxx();
protected:
private:
};
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?
UPDATE: What is a programmer does this:
Class xxx {
public:
xxx();
virtual ~xxx();
public:
more stuff();
more();
ifndef __BUILD_WIN__
public:
even more();
envenmore2();
endif
protected:
private:
};
Memory optimization is a range of techniques related to improving computer memory, such as identifying memory leaks and corruption, to optimize memory usage and increase performance and application usability.
Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed. In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes.
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:
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.
Well the compiler wouldn't have to parse them, so there's that benefit to the compiler. For what that's worth (not very much).
There should be no memory use difference.
Other than that, the only benefit I can think of is there's less cruft for someone reading the code to have to deal with (not that it's particularly burdensome in your example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With