简体   繁体   English

如何减小物体的尺寸

[英]How to reduce the size of an object

I am using Visual C++ to write an app. 我正在使用Visual C ++编写应用程序。 I write CMyObject class and may allocate a lot of instances of CMyObject object, so I want to reduce the size of CMyObject class. 我编写了CMyObject类,并且可能会分配许多CMyObject对象的实例,因此我想减小CMyObject类的大小。

What I can figure out to do is: 我能弄清楚的是:

First, I can use the following code to get the accurate size used by a single instance of CMyObject object: 首先,我可以使用以下代码来获取CMyObject对象的单个实例使用的准确大小:

CMyObject Object;

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

is that correct? 那是对的吗?

Second, to reduce the size of CMyObject, I have several ideas: 其次,为减小CMyObject的大小,我有几个想法:

(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. (1)尽可能将成员函数更改为静态成员函数,因为每个成员函数在CMyObject实例中都将占用一些空间。

(2) Change virtual member function to member function if it is possible, since virtual member function may take more spaces. (2)尽可能将虚拟成员函数更改为成员函数,因为虚拟成员函数可能会占用更多空间。

(3) Eliminate unnecessary member variables to reduce spaces. (3)消除不必要的成员变量以减少空间。

(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. (4)最后,如果(1),(2)和(3)不能很好地工作,则将CMyObject从类更改为仅包含一些成员变量的结构,这样将消除分配给类的构造函数和析构函数的空间。

is my ideas correct? 我的想法正确吗?

Thanks 谢谢

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. 1.如果可能,将成员函数更改为静态成员函数,因为每个成员函数在CMyObject实例中都将占用一些空格。

Entirely incorrect. 完全不正确。 Member functions are not stored in objects. 成员函数未存储在对象中。 At all. 完全没有

2. Change virtual member function to member function if it is possible, since virtual member function may take more spaces. 2.尽可能将虚拟成员函数更改为成员函数,因为虚拟成员函数可能会占用更多空间。

If you remove all virtual member functions in your inheritance tree, you'll prevent vtable pointers from being generated. 如果删除继承树中的所有虚拟成员函数,则将防止生成vtable指针。 That'll save you a few bytes, sure. 当然,这样可以节省一些字节。

3. Eliminate unnecessary member variables to reduce spaces. 3.消除不必要的成员变量以减少空间。

Yes, and if they're unnecessary, they should be removed anyway. 是的,如果不需要,则无论如何都应将其删除。

4. 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. 4. CMyObject从类到仅包含一些成员变量的结构,因此将消除分配给类的构造函数和析构函数的空间。

Again, no; 再说一次 even if structs differed from classes in this regard (they don't), functions still aren't stored in objects. 即使结构在这方面与类有所不同(它们也没有),函数仍未存储在对象中。 Functions are code, not data. 函数是代码,而不是数据。

(1) and (2) aren't valid options. (1)和(2)是无效选项。 Non- virtual methods take up no space per object, virtual methods add no overhead beyond the first one defined in the inheritance hierarchy. virtual方法不占用每个对象空间, virtual方法除了继承层次结构中定义的第一个方法外,不增加开销。

(3) is obviously valid. (3)显然是有效的。

(4), again, invalid. (4)再次无效。 There's no difference, other than default access level, between class and struct . 除了默认访问级别, classstruct之间没有任何区别。

Some other valid suggestions: 其他一些有效建议:

  • reordering of members (or a pragma pack ). 成员的重新排序(或pragma pack )。 You might have padding bytes between members which might give you some overhead. 成员之间可能会有填充字节,这可能会给您带来一些开销。
  • using smaller types ( short instead of int if possible, etc.) 使用较小的类型(如果可能,使用short而不是int等)
  • shared static data members if possible 如果可能,共享static数据成员

sizeof is correct. sizeof是正确的。 Technically it returns std::size_t , not int , but if your object is bigger than INT_MAX then you'd probably know about it already. 从技术上讲,它返回std::size_t ,而不是int ,但是如果您的对象大于INT_MAX那么您可能已经知道它了。

3 is correct. 3是正确的。

1 is not correct. 1不正确。 There is no existing C++ implementation in which member functions take up space in the object. 没有现有的C ++实现,其中成员函数占用对象中的空间。

2 is very slightly correct. 2是非常正确的。 If your class (including all base classes) has no virtual members at all, then you will save roughly the size of a pointer per object. 如果您的类(包括所有基类)根本没有虚拟成员,那么您将节省每个对象的指针大小。 So getting rid of the last one saves space, the others don't. 因此,摆脱最后一个可以节省空间,而其他则不能。

4 is not correct, constructors and destructors are the same as any other member function in this respect. 4是不正确的,在这方面构造函数和析构函数与任何其他成员函数相同。

As well as 3, you could try laying out the data members in a different order. 以及3,您可以尝试以不同的顺序布置数据成员。 The general idea is to work out where the padding will occur in your class and re-arrange the members to reduce it. 总体思路是确定班级中填充的位置,然后重新安排成员以减少填充。 The first rule of thumb is to sort them in order from the one with the greatest alignment requirement to the least, although this is implementation-dependent and anyway that's not the only way to minimize padding. 第一个经验法则是从对齐要求最大的顺序到排序要求最小的顺序对其进行排序,尽管这是与实现相关的,并且无论如何不是最小化填充的唯一方法。 Of course, this changes the order that the members are constructed and destroyed, so it may not be appropriate for your class. 当然,这会更改成员的构造和销毁顺序,因此可能不适用于您的班级。

Only 3 is correct. 只有3是正确的。

Also you can change types of variable. 您也可以更改变量的类型。 For example you can change int to char , if variable < 255. 例如,如果变量<255,则可以将int更改为char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM