CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Posts
    54

    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:

    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?


    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to reduce the size of an object

    Quote Originally Posted by AlanCCC
    1. I can use the following code to get the accurate size used by a single instance of CMyObject object:

    CMyObject Object;

    // Get the size of memory allocated for CMyObject object
    int nSize = sizeof(Object);

    is that correct?
    Yes, but if pointers are involved, then the pointers may point to memory that is not accounted for by sizeof.

    Quote Originally Posted by AlanCCC
    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?
    #1 and #4 have to do with the space for the code, which is likely to remain constant no matter how many objects of the class are created. (It is not included in the result of sizeof anyway.) Note that a struct is a class, so merely changing keyword will make no difference other than to the default access control and inheritance.

    #2 also has to do with the space of the code, but unless you get rid of all the virtual member functions, you will pay the cost of having a virtual table (increasing the size of each object) if the compiler cannot optimise it away. However, if you need such runtime polymorphism, then you have to implement it in some way or another, so you might as well use the language feature implemented by the compiler.

    #3 makes sense, but if those member variables are meant to cache the values of certain computations, then this could come at the cost of efficiency, so you need to decide if the trade-off is worth it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to reduce the size of an object

    Quote Originally Posted by AlanCCC View Post
    I write CMyObject class and may allocate a lot of instances of CMyObject object, so I want to reduce the size of CMyObject class.
    As I said in another thread, this is a dead end. You need to review your logic (calculation, storage, whatever) to be able to deal with a limited subset of the whole array at a time.
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured