In the wake of this question about static methods in managed code, I’m interesting if the answers there is relevant to unmanaged code like c++.
I make thousands of instances, and my question is mainly about static methods. Do this methods save memory compared regular methods?
thank you, and sorry about my poor English.
All methods require their binary code to be in memory in order to run. The executable code for
staticandnon-staticmethods is (largely) the same.Both types of methods require only one place in memory, so they’re not replicated with every instance of the class.
Let’s now take a look at some code:
There are only minor differences, such as pushing the
thispointer onto the stack for the non-static function, but they are minor, and probably a decent optimizer will reduce the differences even further.A decision about whether or not to use static functions should be strictly design-driven, not memory-driven.