February 10th, 2014, 05:46 PM
How to reduce the size of an object
I am using Visual C++ to write an app. I write CMyObject class and may allocate a lot of instances of CMyObject object, so I want to reduce the size of CMyObject class.
What I can figure out to do is:
1. I can use the following code to get the accurate size used by a single instance of CMyObject object:
// Get the size of memory allocated for CMyObject object
int nSize = sizeof(Object);
is that correct?
2. To reduce the size of CMyObject, I have several ideas:
(1) Change member function to static member function if it is possible, since each member function will take some spaces in the instance of CMyObject.
(2) Change virtual member function to member function if it is possible, since virtual member function may take more spaces.
(3) Eliminate unnecessary member variables to reduce spaces.
(4) Finally, if (1), (2) and (3) does not work well, then change CMyObject from class to a struct that only contains some member variables, thus will eliminate the spaces allocated for constructor and destructor of a class.
is my ideas correct?
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.