C ++:我如何知道Base类SubObject的大小? [英] C++ : How can I know the size of Base class SubObject?

查看:209
本文介绍了C ++:我如何知道Base类SubObject的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里我在讨论空基本优化, MSalters 发表了这个有趣的评论: p>


没有类可以有
sizeof(Class)== 0,空或不。 但是
我们正在讨论一个空的基类子对象的
大小。

它不需要自己的vtable,也不需要
vtable指针。假设在偏移
0处的vtable指针的公共
布局;这将导致零大小的
基类子对象与派生类共享其
vtable指针。
没问题:这些应该是相同的
无论如何,这是虚拟函数的
的点。


我的问题具体是这样的:当我们使用空类作为基类时,编译器可能会优化,或者它可能不会。我们将如何确定它实际上是做什么?



一般来说,我们如何知道基类子对象的大小?是不是我们使用它作为基地的Base子对象的大小是否相同?

解决方案

只有空基本类才能优化编译器?



<好的答案。



顺便说一句,MS VC ++和G ++编译器可以转储类布局供您研究。



使用VC ++只需运行 cl.exe / c / d1reportAllClassLayout< source> .cpp



vtable布局转储代码我在1990年写的测试正确的布局对象,vtables,vbtables等。



为了更好地理解C ++编译器如何在内存中布置对象, http://www.openrce.org/articles/files/jangrayhood.pdf 和Stan Lippman的书C ++对象模型里面。



快乐黑客!


.

Here I was discussing Empty Base Optimization, and MSalters made this interesting comment:

No class can ever have sizeof(Class)==0, empty or not. But we're talking specifically over the size of an empty base class subobject. It doesn't need its own vtable, nor a vtable pointer. Assume the common layout of a vtable pointer at offset 0; that would cause the zero-sized base class subobject to share its vtable pointer with the derived class. No problem: those should be identical anyway, that's pretty much the point of virtual functions.

My question specifically is this: compiler may optimize when we use Empty Class as base class, or it may not. How would we determine as to what it actually does?

And in general, how can we know the size of base class subobject? Is the size of Base subobject same irrespective of whether we use it as base or not? Do compilers optimize only with Empty Base Classes?

.

解决方案

Good answers.

By the way, MS VC++ and G++ compilers can dump class layouts for you to study.

With VC++ just run cl.exe /c /d1reportAllClassLayout <source>.cpp

This uses class and vtable layout dump code I wrote in 1990 to test proper layout objects, vtables, vbtables, etc.

To better understand how C++ compilers lay out objects in memory, you might enjoy http://www.openrce.org/articles/files/jangrayhood.pdf and Stan Lippman's book Inside the C++ Object Model.

Happy hacking!

这篇关于C ++:我如何知道Base类SubObject的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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