对象的大小 [英] size of object

查看:61
本文介绍了对象的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




让我们说它是一个32位处理器然后指针的大小是32

位。


现在我想知道带有vtable

指针的类的大小,即它几乎没有虚函数。


因为对象内存的第一个位置指向

vtbale因此应该是32位。


我是对的吗?


请在我的脑海中消除混乱。


此类的大小是多少,没有数据成员只有一个

功能。


问候

Tarun

解决方案



Tarundeep < TA ******* @ gmail.com>在消息中写道>


让我们说它是一个32位处理器然后指针的大小是32位。
现在我想知道具有vtable
指针的类的大小是什么,即它具有很少的虚函数。


我们所讨论的首先是具体的实现。

一般来说,一个类不会持有vtable指针而是每个类的对象

会保留它。

因为对象内存的第一个位置指向
vtbale所以应该是32位。

我是对 ??


不,没有关于由

标准制作的v-tables或内存布局的承诺。一个实现_could_选择让它像你一样

说。

请记住我脑海中的混乱。

大小又是多少没有数据成员和只有一个
函数的类。




标准确保的所有内容都是如果它是
层次结构然后它将具有大于零的大小。 EBCO(空基类

优化)可以用空基类进行描述。


Sharad


< BLOCKQUOTE>" Tarundeep" < TA ******* @ gmail.com>在消息中写道

news:86 ************************* @ posting.google.co m ... < blockquote class =post_quotes>

让我们说它是一个32位处理器,然后指针的大小是32位。


32位处理器上指针的大小可能或者

可能不是32位。

现在我想要要知道具有vtable
指针的类的大小,
即它几乎没有虚函数。


以下使用''size''的C ++概念,如'sizeof''所报告的那样
,即以字节为单位。 br />

类类型对象的大小不受该类具有的许多成员函数的影响,无论是否虚拟。

类型对象的大小至少是其数据成员大小的总和(但可能更大,

,因为填充是成员之间允许);或者没有任何

数据成员,大于零。


因为对象内存的第一个位置指向
vtbale因此应该是32位。


类型对象的地址与第一个声明的数据成员的地址相同。

,如果声明了一个地址。

''vtable''不是该语言的一部分,它是一种实现用于实现多态的一些机制。

我是对的?


不。 :-)

请删除我心中的困惑。

同样没有数据成员和只有一个
功能的类的大小是什么。




成员函数的存在或数量不会影响类类型对象的

大小。

类型对象的大小没有数据成员将大于零

(确切的大小留给实现)。


摘要:对象的实际物理实现(例如,作为多态类型之一的
)可能会消耗比'/ b $ b报告的更多存储空间''的sizeof'。这个额外的''开销''不被认为是对象'的大小或表示的一部分,也不是应用程序'

地址空间的一部分。 />

-Mike


Mike Wahler写道:

" Tarundeep" < TA ******* @ gmail.com>在消息中写道
新闻:86 ************************* @ posting.google.co m ...



让我们说它是32位处理器,然后指针的大小是32位。
32位指针的大小处理器可能或者
可能不是32位。


现在我想知道具有vtable
指针的类的大小是多少它,它几乎没有虚函数。



以下使用''size''的C ++概念,如'sizeof'所报道的那样',即以字节为单位测量。

类类型对象的大小不受类具有的许多成员函数(虚拟或非虚拟)的影响。




实际上,OP可能并不遥远。在许多实现中,

(特别是基于vtable的),类的sizeof(T)受到的影响是/ b $ b虚拟函数的存在或多或少与OP的方式相同假设。

下面的非便携式技巧,可以对多态的

类进行编译时检查依赖于它:


模板< unsigned long M,unsigned long N>

class CHECK_is_equal {

private:


template< unsigned long A>

class XXX {

public:


void operator ==(const XXX&)const { };


};


public:


CHECK_is_equal(void){

XXX< M> a;

XXX< N> b;

a == b;

}

};


模板< typename T>

class CHECK_is_polymorphic {

private:


class NonVirtual:public T {

public:


~NonVirtual(void){}



class Virtual :public T {

public:


virtual~Virtual(void){}

};


公开:


CHECK_is_polymorphic(无效){

CHECK_is_equal< sizeof(非虚拟),sizeof(虚拟)> x;

}

};


这是有效的,因为vtable指针消耗了对象中的一些空间。至于

,据我所知,boost :: is_polymorphic<>使用这个想法的改进。但是,
同意,它并不是标准所保证的。


类型对象的大小至少为
其数据成员的大小总和(但可能更大,因为成员之间允许填充);或者缺少大于零的任何数据成员。

由于对象内存的第一个位置指向
vtbale因此应该32位。



类型对象的地址与第一个声明的数据成员的地址相同,如果声明了一个地址。
''vtable''不是语言的一部分,它是一些实现用于实现多态的机制。


我是对的?



不。 :-)


请删除我心中的困惑。

同样没有数据成员且只有一个的类的大小是多少
函数。



成员函数的存在或数量不会影响类类型对象的大小。没有数据成员的类类型的对象的大小将大于零
(确切的大小留给实现)。

摘要:实际的物理实现一个对象(例如多态类型之一)可能会消耗比'sizeof'报告的更多的存储空间。这个额外的开销不被视为对象的大小或表示的一部分,也不是应用程序的地址空间的一部分。




我怀疑类T的对象是否允许使用比


malloc(sizeof(T))更多的内存;


分配。然而,我可能会产生误解,因为我不清楚你的意思是开销是什么意思。 不是

被认为是对象的大小或表示的一部分,也不是

应用程序的地址空间的一部分。据我所知,

编译器需要坚持使用

标准规定的对象的所有内容都是其表示的一部分,尽管标准没有声明编译器在那里可能存在的那些东西。

但是,如果编译器发现它很容易粘在vtable指针中,那么/>
然后我没有看到标准中的任何语言,sizeof()会阻止

来计算该指针。 (然后,标准很长;而且我可能会遗漏一些东西。)

最好


Kai-Uwe Bux


hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.

now i want to know what would be the size of the class with vtable
pointer in it , that is it has few virtual functions.

Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.

Am i right ??

Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.

Regards
Tarun

解决方案


"Tarundeep" <ta*******@gmail.com> wrote in message > hi,


let us say it is a 32 bit processor then the size of the pointer is 32
bits.

now i want to know what would be the size of the class with vtable
pointer in it , that is it has few virtual functions.
First of all things we are talking about are implementation specific.
Generally a class would not hold a vtable pointer but each object of class
would hold it.
Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.

Am i right ??
No, there is no such promise about v-tables or memory layout made by the
Standard. An implementation _could_ choose to have it the way you are
saying.
Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.



All that the standard ensures is that if it is the most derived class in a
hierarchy then it will have a size greater than zero. EBCO (Empty base class
optimization) can come into picture with empty base classes.

Sharad


"Tarundeep" <ta*******@gmail.com> wrote in message
news:86*************************@posting.google.co m...

hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.
The size of a pointer on a 32-bit processor might or
might not be 32 bits.

now i want to know what would be the size of the class with vtable
pointer in it ,
that is it has few virtual functions.
The following uses the C++ notion of ''size'', as reported
by ''sizeof'', that is, measured in bytes.

The size of an object of class type is not affected by how
many member functions the class has, virtual or not.

The size of an object of class type will be at least the
sum of the sizes of its data members (but possibly larger,
since padding is allowed between members); or lacking any
data members, greater than zero.

Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.
The address of an object of class type will be the same address
as that of its first declared data member, if one is declared.
''vtable'' is not part of the language, it''s a mechanism some
implementations use to achieve polymorphism.

Am i right ??
Nope. :-)

Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.



The existence or number of member functions does not affect the
size of an object of class type. The size of an object of
class type which has no data members will be greater than zero
(exact size left up to the implementation).

Summary: The actual physical implementation of an object (such
as one of a polymorphic type) might consume more storage than
that reported by ''sizeof''. This extra ''overhead'' is not considered
part of the object''s size or representation, nor part of the application''s
address space.

-Mike


Mike Wahler wrote:

"Tarundeep" <ta*******@gmail.com> wrote in message
news:86*************************@posting.google.co m...

hi,

let us say it is a 32 bit processor then the size of the pointer is 32
bits.
The size of a pointer on a 32-bit processor might or
might not be 32 bits.


now i want to know what would be the size of the class with vtable
pointer in it ,
that is it has few virtual functions.



The following uses the C++ notion of ''size'', as reported
by ''sizeof'', that is, measured in bytes.

The size of an object of class type is not affected by how
many member functions the class has, virtual or not.



Actually, the OP might be not that far off. In many implementations
(especially vtable based ones), the sizeof(T) for a class is affected by
the presence of virtual functions more or less in the way the OP assumes.
The following non-portable trick to do a compile time check for polymorphic
classes relies on it:

template < unsigned long M, unsigned long N >
class CHECK_is_equal {
private:

template < unsigned long A >
class XXX {
public:

void operator== ( const XXX & ) const {};

};

public:

CHECK_is_equal ( void ) {
XXX< M > a;
XXX< N > b;
a == b;
}
};

template < typename T >
class CHECK_is_polymorphic {
private:

class NonVirtual : public T {
public:

~NonVirtual ( void ) {}

};

class Virtual : public T {
public:

virtual ~Virtual ( void ) {}

};

public:

CHECK_is_polymorphic ( void ) {
CHECK_is_equal< sizeof( NonVirtual ), sizeof( Virtual ) > x;
}
};

This works because the vtable pointer consumes some space in the object. As
far as I know, boost::is_polymorphic<> uses a refinement of this idea. I
agree, however, that it is by no means guaranteed by the standard.

The size of an object of class type will be at least the
sum of the sizes of its data members (but possibly larger,
since padding is allowed between members); or lacking any
data members, greater than zero.

Since the very first location of the object memory points to the
vtbale hence that should be 32 bit.



The address of an object of class type will be the same address
as that of its first declared data member, if one is declared.
''vtable'' is not part of the language, it''s a mechanism some
implementations use to achieve polymorphism.


Am i right ??



Nope. :-)


Pls remove the confusion in my mind.

Also what is the size of the class with no data member and just one
function.



The existence or number of member functions does not affect the
size of an object of class type. The size of an object of
class type which has no data members will be greater than zero
(exact size left up to the implementation).

Summary: The actual physical implementation of an object (such
as one of a polymorphic type) might consume more storage than
that reported by ''sizeof''. This extra ''overhead'' is not considered
part of the object''s size or representation, nor part of the application''s
address space.



I doubt that objects of class T are allowed to use more memory than

malloc( sizeof(T) );

allocates. However, there could be a misunderstanding on my part, because I
am not really clear about what you mean by "overhead" that "is not
considered part of the object''s size or representation, nor part of the
application''s address space". As far as I can tell, everything that the
compiler needs to stick into an object to make it work as prescribed by the
standard is part of its representation, although the standard makes no
claims as to what those tings might be that compiler sticks in there.
However, if a compiler finds it convenient to stick in a vtable pointer,
then I do not see any language in the standard that would sizeof() prevent
from accounting for that pointer. (Then, again, the standard is long; and I
might be missing something.)
Best

Kai-Uwe Bux


这篇关于对象的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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