C ++:获得最大类的大小的聪明方法? [英] C++: Smart way to get the size of the largest class?

查看:92
本文介绍了C ++:获得最大类的大小的聪明方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我想知道在编译时是否有任何好方法可以获得某些类的最大大小。问题的背景是我正在使用对象池我需要设置我将添加的最大对象的大小。



目前我有一个其他项目(我首先编译)是我打印所有类的sizeof并选择其中最大的,但是它有点累,因为我太频繁地添加和删除类以方便。我知道我可以使用一些临时尺寸直到发布,但我只是想知道是否真的有必要。



如果你有任何好主意我会真的感谢它。



******************************* ********************************** *



谢谢大家的好消息!他们帮助我了解如何从这里继续。



******************** *********************************************



http://www.cplusplus开头找到我想要的内容.com / articles / EhvU7k9E / [ ^ ]即使我已经有了一些(可能更好)的建议。不幸的是,尽管Visual Studio仍然不支持Variadic模板,所以现在它不是一个选项。



Hi!

I wonder if there is any good way to get the largest size of some classes at compile time. The background to the problem is that I'm using an object pool were I need to set the size of the largest object that I will add.

At the moment I have an other project (which I first compile) were I print sizeof for all classes and pick the largest of them, however it's a bit exhausting since I add and remove classes too frequently for that to be conveniently. I'm aware of that I could use some temporary size until release but I was just wondering if that really was necessary.

If you have any good ideas I would really appreciate it.

*****************************************************************

Thank you all for the great answers! They have helped me a lot to understand how to continue from here.

*****************************************************************

Found exactly what I was looking for from the beginning at http://www.cplusplus.com/articles/EhvU7k9E/[^] even if I have got some (probably better) suggestions already. Unfortunately though Visual Studio still doesn't support Variadic templates so it's not an option at the moment anyway.

template<typename... Arguments>
class VariadicTemplate{
private:
    static const unsigned short int size = sizeof...(Arguments);
};

推荐答案

如果它只是某些类,你可以定义它们的并集并取大小联盟。这将给你最大的联盟成员的大小。在编译时甚至可以知道这个大小。

If it's just "some classes" you can define a union of them and take the size of the union. That will give you the size of the biggest member of the union. And that size is even known at compile time.
union U
{
   ClassA a;
   ClassB b;
   ClassC c;
};

size_t biggestSize = sizeof (U);


为什么不能为每个类使用分配池,而不是为最大的类分配?如果多个类的大小相同,你可以使用模板让它们使用相同的池。



我以前做过这种事情并且它有效好。注意对齐问题。每个分配单元大小应为8的倍数,并与双字边界对齐。例如,如果您的类的大小为7,则需要将分配大小四舍五入为(8的倍数)。
Instead of allocating for the largest sized class, why can't you use an allocation pool for each class? If more than one class is the same size, you could use templates to have them use the same pool.

I've done this kind of thing before and it works well. Beware of alignment issues. Each allocation unit size should be a multiple of 8, and aligned to a double word boundary. If your class is of size 7 for example you need to round up the allocation size to (a multiple of) 8.


C ++无法获取类似的统计信息。你。



你必须写它。



在你的项目中你可以有一个单身人士跟踪大小或最大大小的类,然后查询每个类的大小。



除非您需要值,否则我认为不需要单独的应用程序在编译时。



在遥远的过去,当我需要这种缓冲池时,我创建了多个,每个都有一个预定的大小。然后在分配一个对象时,我选择了一个大小足以容纳它的池。



将池分配为2的幂可能对你有用。



*********************



如解决方案3中所述...



四舍五入可以让你在游泳池大小上有很好的传播。



这是一个方便的代码片段。

There is no way provided by C++ to get statistical information like that for you.

You'll have to write it.

Inside your project you could have a singleton class that tracks the sizes, or the largest size, then query the sizeof each class.

I don't see a need for a separate app unless you need the value at compile time.

In the distant past when I needed buffer pools of this sort, I created more than one, each with a predetermined size. Then when allocating an object, I picked the pool that had a size that is just big enough to hold it.

Allocating pools as powers of 2 might work for you.

*********************

As mentioned in Solution 3...

Rounding up can give you a good spread on the pool size.

Here's a handy code fragment for doing this.
auto newSize = ((((size - 1) >> 4) + 1) << 4);





这个代码片段的作用是将大小四舍五入到最接近的16的倍数。

用5替换4会将其舍入为32的倍数,因为它是基数2.



What this code fragment does, is round up the size to the nearest multiple of sixteen.
Replacing the 4 with a 5 would round up to a multiple of 32 as it's base 2.


这篇关于C ++:获得最大类的大小的聪明方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆